You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a WebSocket server, if you do push on a disconnected client, you will get an ErrorException with message "Swoole\WebSocket\Server::push(): the connected client of connection[1] is not a websocket client or closed".
It would be nice if instead that was some custom exception so you could catch it.
For example:
For now you can do it like this, which is works, but I think option with custom exception is better and potentially more performant, because push function already checks connection before pushing in to it
$server->on('Message', staticfunction (Server$server, Frame$frame) {
if ($server->isEstablished($frame->fd)) {
$server->push($frame->fd, 'Test');
} else {
/* do something */
}
});
The text was updated successfully, but these errors were encountered:
We prefer second option where we check $server->isEstablished($frame->fd) before $server->push($frame->fd, 'Test'); The reason is: Most of the time we use
if ($server->isEstablished($frame->fd)) {
$server->push($frame->fd, 'Test');
... inside a loop in order to broadcast particular data to multiple connections (users), where issuing an Exception on a disconnected client can cause the loop to break resulting in failure to send data to the connections next in queue (to be iterated in next iterations of broadcasting-loop)
In a WebSocket Service a connection may disconnect, close or disconnected at anytime; even during the time when we are iterating through the Broadcasting loop (a around code above). An exception will break the loop and will stop sending data to all other connections (only due to a closed / disconnected connection, in the middle of connections queue). This seems not a good idea.
Disconnection / closing of a Socket connection (from client OR server side) is not actually an Error Or Exception, but an allowable and common operation.
In a WebSocket server, if you do push on a disconnected client, you will get an ErrorException with message "Swoole\WebSocket\Server::push(): the connected client of connection[1] is not a websocket client or closed".
It would be nice if instead that was some custom exception so you could catch it.
For example:
For now you can do it like this, which is works, but I think option with custom exception is better and potentially more performant, because push function already checks connection before pushing in to it
The text was updated successfully, but these errors were encountered: