hoaproject / Websocket

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

Try to change the stream resource with an invalid one; given unknown type when trying to broadcastIf in on close. #100

Open undsoft opened 7 years ago

undsoft commented 7 years ago

Hi,

I have several JS clients connected to websocket server. They are organized into channels (I store Node objects). Now one of the clients (client A) disconnects. In PHP server, I'm listening to 'close' event and want to tell other clients in that room that A has disconnected.

Here's my code:

$this->websocket->on('close', function (Bucket $bucket) {
    // Get list of connected channel clients
    // Make sure to remove disconnected client (client A) from the list.
    $this->websocket->broadcastIf(function ($currentNode) use ($channelNodes) {
        $result = false;

        /** @var \Hoa\Websocket\Node $currentNode */
        /** @var \Hoa\Websocket\Node $node */
        foreach ($channelNodes as $node) {
            if ($currentNode->getId() === $node->getId()) {
                $result = true;
                break;
            }
        }

        return $result;
    }, Json::encode($data));
});

However, when I do this I get this message:

[2017-09-13 11:50:36.335700] Try to change the stream resource with an invalid one; given unknown type. at /Users/undsoft/htdocs/freelancer/vendor/hoa/stream/Stream.php. Line 412
#0 /Users/undsoft/htdocs/freelancer/vendor/hoa/socket/Connection/Handler.php(276): Hoa\Stream\Stream->_setStream(Resource id #170)
#1 /Users/undsoft/htdocs/freelancer/vendor/hoa/websocket/Connection.php(566): Hoa\Socket\Connection\Handler->Hoa\Socket\Connection\{closure}(1, true)
#2 [internal function]: Hoa\Websocket\Connection->send('{"type":"connec...', Object(Hoa\Websocket\Node))
#3 /Users/undsoft/htdocs/freelancer/vendor/hoa/socket/Connection/Handler.php(336): call_user_func_array(Array, Array)
#4 /Users/undsoft/htdocs/freelancer/websockets/classes/Handler.php(345): Hoa\Socket\Connection\Handler->broadcastIf(Object(Closure), '{"type":"connec...')

Any ideas?

undsoft commented 7 years ago

I can work around it by marking client as disconnect in on close and then actually telling everybody about it in on message. Still would be nice to fix this or at least document it.

Hywan commented 7 years ago

Hello,

I am in the opposite side of the world right now and I can't work on this issue. Maybe @vonglasow or @Pierozi?