commit
This commit is contained in:
parent
d6b6182c39
commit
0b8e09e39e
|
|
@ -7,9 +7,8 @@ use Illuminate\Http\Request;
|
|||
|
||||
class EventController extends EchoController
|
||||
{
|
||||
|
||||
/** @var ChannelManager */
|
||||
private $channelManager;
|
||||
protected $channelManager;
|
||||
|
||||
public function __construct(ChannelManager $channelManager)
|
||||
{
|
||||
|
|
@ -18,8 +17,19 @@ class EventController extends EchoController
|
|||
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
//TODO: verify the incoming request
|
||||
/*
|
||||
* array:6 [
|
||||
"appId" => "test"
|
||||
"auth_key" => ""
|
||||
"auth_signature" => "51e7ab9c1411aacf9a4c28001ffc3e7f5fe71db130ce08ac071ab49d737bcf52"
|
||||
"auth_timestamp" => "1542833998"
|
||||
"auth_version" => "1.0"
|
||||
"body_md5" => "816e28da10f4aedf0821865eddf55e7f"
|
||||
]
|
||||
*/
|
||||
foreach ($request->json()->get('channels', []) as $channelId) {
|
||||
$channel = $this->channelManager->find($channelId);
|
||||
$channel = $this->channelManager->find($request->appId, $channelId);
|
||||
|
||||
$channel->broadcast([
|
||||
'channel' => $channelId,
|
||||
|
|
|
|||
|
|
@ -7,12 +7,13 @@ use Ratchet\ConnectionInterface;
|
|||
|
||||
class Channel
|
||||
{
|
||||
/** @var string */
|
||||
protected $channelId;
|
||||
|
||||
/** @var ConnectionInterface[] */
|
||||
protected $connections = [];
|
||||
|
||||
public function __construct($channelId)
|
||||
public function __construct(string $channelId)
|
||||
{
|
||||
$this->channelId = $channelId;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,28 +5,39 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
|
|||
|
||||
class ChannelManager
|
||||
{
|
||||
/** @var array */
|
||||
protected $channels = [];
|
||||
|
||||
public function findOrCreate(string $channelId): Channel
|
||||
/** @var string */
|
||||
protected $appId;
|
||||
|
||||
public function findOrCreate(string $appId, string $channelId): Channel
|
||||
{
|
||||
if (! isset($this->channels[$channelId])) {
|
||||
|
||||
if (! isset($this->channels[$appId][$channelId])) {
|
||||
|
||||
/**TODO: make this variable to go away */
|
||||
$channelClass = $this->detectChannelClass($channelId);
|
||||
$this->channels[$channelId] = new $channelClass($channelId);
|
||||
|
||||
array_set($this->channels, "{$appId}.{$channelId}", new $channelClass($channelId));
|
||||
|
||||
}
|
||||
|
||||
return $this->channels[$channelId];
|
||||
return $this->channels[$appId][$channelId];
|
||||
}
|
||||
|
||||
public function find(string $channelId)
|
||||
public function find(string $appId, string $channelId)
|
||||
{
|
||||
return $this->channels[$channelId] ?? null;
|
||||
return $this->channels[$appId][$channelId] ?? null;
|
||||
}
|
||||
|
||||
protected function detectChannelClass($channelId) : string
|
||||
{
|
||||
if (starts_with($channelId, 'private-')) {
|
||||
return PrivateChannel::class;
|
||||
} elseif(starts_with($channelId, 'presence-')) {
|
||||
}
|
||||
|
||||
if(starts_with($channelId, 'presence-')) {
|
||||
return PresenceChannel::class;
|
||||
}
|
||||
return Channel::class;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ class EchoServer extends WebSocketController
|
|||
*/
|
||||
function onOpen(ConnectionInterface $conn)
|
||||
{
|
||||
|
||||
dump("Client connected");
|
||||
/**
|
||||
* There are a couple things we need to do here:
|
||||
|
|
@ -35,6 +36,14 @@ class EchoServer extends WebSocketController
|
|||
// Store the socketId along with the connection so we can retrieve it.
|
||||
$conn->socketId = $socketId;
|
||||
|
||||
/** @var \GuzzleHttp\Psr7\Request $request */
|
||||
$request = $conn->httpRequest;
|
||||
|
||||
$queryParameters = [];
|
||||
parse_str($request->getUri()->getQuery(), $queryParameters);
|
||||
|
||||
$conn->appId = $queryParameters['appId'];
|
||||
|
||||
$conn->send($this->buildPayload('pusher:connection_established', [
|
||||
'socket_id' => $socketId,
|
||||
'activity_timeout' => 60,
|
||||
|
|
@ -58,7 +67,8 @@ class EchoServer extends WebSocketController
|
|||
}
|
||||
} else {
|
||||
// Try to find a channel and broadcast the message to the clients.
|
||||
$channel = $this->channelManager->find($payload->channel);
|
||||
$channel = $this->channelManager->find($conn->appId, $payload->channel);
|
||||
|
||||
if ($channel) {
|
||||
$channel->broadcast($payload);
|
||||
}
|
||||
|
|
@ -82,7 +92,8 @@ class EchoServer extends WebSocketController
|
|||
*/
|
||||
protected function pusherSubscribe(ConnectionInterface $conn, $payload)
|
||||
{
|
||||
$channel = $this->channelManager->findOrCreate($payload->channel);
|
||||
$channel = $this->channelManager->findOrCreate($conn->appId, $payload->channel);
|
||||
|
||||
$channel->subscribe($conn, $payload);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue