hoaproject / Websocket

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

Problem when using broadcast() in the 'close' event #18

Closed Geslain closed 10 years ago

Geslain commented 10 years ago

Hi,

When i try to Use the broadcast() function on a close event in the server, if a client disconnect himself, the first remaining connected client gets disconnected and he's the only one to receive the broadcast message . Others don't receive anything.

$websocket->on('close', function ( Hoa\Core\Event\Bucket $bucket )  {
    echo "connection closed\n";
    $bucket->getSource()->broadcast("A client disconnect himself from server"));
    return;
});

To understand, here's an example :

thehawk970 commented 10 years ago

In addition an little MWE for explain that :) https://gist.github.com/camael24/9c601fb14ee003ac7896

osaris commented 10 years ago

I've tested the @camael24 gist and I can reproduce the problem. I tried to debug but can't find where is the problem :-(

osaris commented 10 years ago

While discussing with @Geslain we found that the problem seems to come from :

https://github.com/hoaproject/Socket/blob/c0820fa8eee12d2d375818fe5877a0853d6a3246/Connection/Handler.php#L257

Because $old is a resource of type unknown (and we except stream) when the client disconnect. It break, disconnect the current client and stop the broadcast.

If you comment these lines

https://github.com/hoaproject/Stream/blob/d1d2388abd3581dff63a44dff9f224c006e06c21/Stream.php#L410-L413

there is no problem anymore (but it's not a solution !).

Geslain commented 10 years ago

A temporary solution seems to be surrounding the _setStream($old) with try catch instructions :

https://github.com/hoaproject/Socket/blob/c0820fa8eee12d2d375818fe5877a0853d6a3246/Connection/Handler.php#L267

return function ( ) use ( &$send, &$old, &$self ) {

    $out = call_user_func_array($send, func_get_args());
    try {
        $self->getConnection()->_setStream($old);
    } catch (\Hoa\Core\Exception\Exception $e) {
        echo 'Get Exception : ',  $e->getMessage(), "\n";
    }

    return $out;
};
Hywan commented 10 years ago

This patch https://github.com/hoaproject/Stream/pull/4 on Hoa\Stream should fix your issue. Can you confirm please?

Geslain commented 10 years ago

Working for me too

Hywan commented 10 years ago

I am asking the review of @osaris and @camael24 also :-).

osaris commented 10 years ago

Tested and it works. Good job !

thehawk970 commented 10 years ago

Same hère

jeudi 24 juillet 2014 19:05 +0200 de Raphaël Emourgeon notifications@github.com: Tested and it works. Good job ! — Reply to this email directly or view it on GitHub .

Hywan commented 10 years ago

Closed by https://github.com/hoaproject/Stream/pull/4.