WebSocket logic

This commit is contained in:
2024-03-13 02:37:46 +01:00
parent dc98f70a06
commit 2392ee360c
4 changed files with 94 additions and 6 deletions

View File

@ -3,6 +3,7 @@
namespace NoccyLabs\Mercureact\Http\Middleware;
use NoccyLabs\Mercureact\Broker\TopicManager;
use NoccyLabs\Mercureact\Broker\WsSubscriber;
use NoccyLabs\Mercureact\Configuration;
use NoccyLabs\React\WebSocket\WebSocketConnection;
use NoccyLabs\React\WebSocket\WebSocketMiddleware;
@ -59,13 +60,16 @@ class WebSocketHandler
{
$this->webSocketClients->attach($connection);
$connection->on('close', function () use ($connection) {
$request = $connection->getServerRequest();
$subscriber = new WsSubscriber($connection, $request, $this->topicManager);
$connection->on('close', function () use ($connection, $subscriber) {
$this->webSocketClients->detach($connection);
$this->topicManager->unsubscribe($subscriber);
});
$request = $connection->getServerRequest();
$topic = $request->getQueryParams()['topic'][0]??'';
$connection->setGroup($topic);
$this->topicManager->subscribe($subscriber, []);
}
}