Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Cannot find how to send message to customized Node #113

Open
MargotDumoulin opened this issue Dec 13, 2020 · 0 comments
Open

Cannot find how to send message to customized Node #113

MargotDumoulin opened this issue Dec 13, 2020 · 0 comments

Comments

@MargotDumoulin
Copy link

MargotDumoulin commented Dec 13, 2020

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant