wip
This commit is contained in:
parent
f279261b57
commit
6768eed53f
|
|
@ -18,6 +18,11 @@ class Channel
|
|||
$this->channelId = $channelId;
|
||||
}
|
||||
|
||||
public function hasConnections(): bool
|
||||
{
|
||||
return count($this->connections) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
|
||||
*
|
||||
|
|
@ -35,6 +40,11 @@ class Channel
|
|||
]));
|
||||
}
|
||||
|
||||
public function unsubscribe(ConnectionInterface $connection)
|
||||
{
|
||||
unset($this->connections[$connection->socketId]);
|
||||
}
|
||||
|
||||
protected function saveConnection(ConnectionInterface $conn)
|
||||
{
|
||||
$this->connections[$conn->socketId] = $conn;
|
||||
|
|
@ -53,6 +63,4 @@ class Channel
|
|||
return $connection->socketId === $conn->socketId;
|
||||
})->each->send(json_encode($payload));
|
||||
}
|
||||
|
||||
//TODO: add unsubscribe
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@
|
|||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
|
||||
|
||||
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class ChannelManager
|
||||
{
|
||||
/** @var array */
|
||||
|
|
@ -39,4 +41,19 @@ class ChannelManager
|
|||
}
|
||||
return Channel::class;
|
||||
}
|
||||
|
||||
public function removeFromAllChannels(ConnectionInterface $connection)
|
||||
{
|
||||
collect($this->channels[$connection->appId])->each->unsubscribe($connection);
|
||||
|
||||
collect($this->channels[$connection->appId])
|
||||
->reject->hasConnections()
|
||||
->each(function (Channel $channel, string $channelId) use ($connection) {
|
||||
unset($this->channels[$connection->appId][$channelId]);
|
||||
});
|
||||
|
||||
if (count($this->channels[$connection->appId]) === 0) {
|
||||
unset($this->channels[$connection->appId]);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,13 @@ class PresenceChannel extends Channel
|
|||
//TODO: send member_added message back to client, and broadcast to everyone on channel
|
||||
}
|
||||
|
||||
public function unsubscribe(ConnectionInterface $connection)
|
||||
{
|
||||
parent::unsubscribe($connection);
|
||||
|
||||
//TODO: send member_removed message back to client, and broadcast to everyone on channel
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use Ratchet\ConnectionInterface;
|
|||
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||
use BeyondCode\LaravelWebSockets\WebSocketController;
|
||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
|
||||
use stdClass;
|
||||
|
||||
class EchoServer extends WebSocketController
|
||||
{
|
||||
|
|
@ -75,6 +76,11 @@ class EchoServer extends WebSocketController
|
|||
}
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $connection)
|
||||
{
|
||||
$this->channelManager->removeFromAllChannels($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://pusher.com/docs/pusher_protocol#ping-pong
|
||||
* @param ConnectionInterface $conn
|
||||
|
|
@ -97,6 +103,14 @@ class EchoServer extends WebSocketController
|
|||
$channel->subscribe($conn, $payload);
|
||||
}
|
||||
|
||||
public function pusherUnsubscribe(ConnectionInterface $connection, stdClass $payload)
|
||||
{
|
||||
$channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel);
|
||||
|
||||
$channel->unsubscribe($connection);
|
||||
|
||||
}
|
||||
|
||||
protected function buildPayload($event, $data = [])
|
||||
{
|
||||
return json_encode([
|
||||
|
|
|
|||
Loading…
Reference in New Issue