hoaproject / Websocket

The Hoa\Websocket library.
https://hoa-project.net/
423 stars 75 forks source link

Cannot find how to send message to customized Node #113

Open MargotDumoulin opened 3 years ago

MargotDumoulin commented 3 years ago

Hello!

I'm working on a project that manages Quizzs. For this project, I need to send message to specific users that are stored in different rooms. One room is made of two users. Each time one user sends a message to the WebSocket server, the server needs to send a message to both users in the room.

The beginning of my code looks like that:

$rooms = [];

$server = new Hoa\Socket\Server('ws://127.0.0.1:8080');
$websocket = new Hoa\Websocket\Server($server);

$websocket->getConnection()->setNodeName(QuizzDuoNode::class);

$websocket->on('open', function (Hoa\Event\Bucket $bucket) {
    echo 'new connection', "\n";

    return;
});

And If I simplify my code, it looks something like this:

$websocket->on('message', function (Hoa\Event\Bucket $bucket) {
    global $rooms;
    global $server;

    $data = $bucket->getData();
    $msgInfo = json_decode($data['message'], true);
    $node = $bucket->getSource()->getConnection()->getCurrentNode();
    $node->setId($msgInfo['id']);

    $nodes = $server->getNodes();

    foreach ($rooms as $room) {
        foreach($nodes as $userNode) {
            if ($userNode->getId() === $room["user1"]) {
                $userNode->send(... speficic message for user1...);
            } else if ($userNode->getId() === $room["user2"]) {
                $userNode->send(... specific message for user2...);
            }
        }
    }

    return;
});

But If I try to use the send() method with a customized node, I get an error message saying that this method doesn't exist in QuizzDuoNode, which is true. The send method is not inherited from the Hoa\Websocket\Node, and I cannot find anything related to this in the documentation.


My problem

I cannot find anything in the documentation that would allow me to send a message to a specific user and I don't know how to do that.