GithubHelp home page GithubHelp logo

WebSocket: After calling $webSockerServer->push() from within onMessage event, the WebSocket connection closes by calling onClose event. about swoole-src HOT 8 OPEN

fakharksa avatar fakharksa commented on July 19, 2024
WebSocket: After calling $webSockerServer->push() from within onMessage event, the WebSocket connection closes by calling onClose event.

from swoole-src.

Comments (8)

NathanFreeman avatar NathanFreeman commented on July 19, 2024

You need to check if the third parameter $reactorId in the onClose event is -1 to determine if the connection was closed by the client or the server.

from swoole-src.

fakharksa avatar fakharksa commented on July 19, 2024

@NathanFreeman
I echoed third parameter like below:

        $this->server->on('Close', function($server, $fd, $reactorId) {
            echo "client {$fd} closed\n ReactorId:{$reactorId}";
        });

and got:

client 1 closed
ReactorId:2

In order to keep a connection opened, do i need to push() (stream) some data continously from server-side (or from the client-side) by puting push() (or send() ) inside a loop ? If the answer is "Yes" then what i will take from this is that push() only sends the data (from the server to the client) and then websocket connection is closed after every push.

from swoole-src.

NathanFreeman avatar NathanFreeman commented on July 19, 2024

$reactorId greater than 0 indicates that the client has actively closed the connection. You need to check the client's code.

from swoole-src.

fakharksa avatar fakharksa commented on July 19, 2024

@NathanFreeman
I think that the issue is in int $flags of the Server::pack() function.

For the (WebSocket's) client-side, I am using this gist (with swoole version 5.1.2)

https://gist.github.com/NHZEX/946275e9b32a832d579a36c5d3d0b7fc

If you just look at this code, the $this->socket->send() function is passing four parameters to Server::pack() (as shown in function below at the end, so initially it was not working). I removed the fourth parameter in order to make the code work (by conforming to the definition of Server::pack(), as per documentation), however i found two things that;

A) That, the third parameter (which is int $flags) work only for value "true", or non-zero value, but not for "false" or 0 ) which means i can not set $flag a value other than "WEBSOCKET_FLAG_FIN" (same is also on server-side) and ...
B) That, the client connection is closed after push() (as reported in this Issue)

So it looks like that the problem lies in how you deal internally with the value of the argument int $flags (as passed in the function Server::pack() to make a Frame object).

//Original Function
    public function send($data, $type = 'text', $masked = false)
    {
        switch ($type) {
            case 'text':
                $_type = WEBSOCKET_OPCODE_TEXT;
                break;
            case 'binary':
            case 'bin':
                $_type = WEBSOCKET_OPCODE_BINARY;
                break;
            case 'ping':
                $_type = WEBSOCKET_OPCODE_PING;
                break;
            default:
                return false;
        }
        return $this->socket->send(Server::pack($data, $_type, true, $masked));
    }

In code above, i changed ...

// see, four parameters (may be because it is code targeting older version of swoole)
return $this->socket->send(Server::pack($data, $_type, true, $masked));

to ...

// Three parameters instead of four
return $this->socket->send(Server::pack($data, $_type, true));

It works with only "true" and "non-zero" values not with "false" and 0. (and parhaps passing the "true" and "non-zero" value also disconnects the connection, just as you say that disconnection is coming from client-side of code; May be this is because when we pass "true" or "non-zero" value then it is same as the default value of int $flags (which is "WEBSOCKET_FLAG_FIN"), whereas if i try to pass a Zero value or a boolean false, i get error as it takes the flow of code into ....
if ($data === false) { } , condition inside function recv() of this gist.

So, again the question is:

How to use server::pack(), and push() (with new type of paramater "int $flags") such that the WebSocker connection do not auto disconnect ?

from swoole-src.

fakharksa avatar fakharksa commented on July 19, 2024

Any update on this issue ? Please, also see my last reply.

@NathanFreeman @matyhtf @twose

from swoole-src.

fakharksa avatar fakharksa commented on July 19, 2024

@NathanFreeman

I have found that there comes an issue in your client-side recv() function of socket object ...

$this->socket->recv();

When server-side pushes all the messages, the function recv() returns not only "false" (false is just fine, as per documentation) but it also issues a warning as below:

PHP Warning: Swoole\Client::recv(): recv() failed, Error: Resource temporarily unavailable[11] in /var/www/html/swoole-prac/websocketclient/wbsocketclient.php on line 87"

And, it also gives a warning that there is no property $errMsg on socket object.

Reference to Client-side Code:
Please, see on line 85 of this gist which i am using now.

from swoole-src.

XJIOP avatar XJIOP commented on July 19, 2024

Same problem after update from v4.8.12 to 5.1.3, push($fd, $data) works every other time.

from swoole-src.

fakharksa avatar fakharksa commented on July 19, 2024

@XJIOP

Way around:

  1. I think push() on server-side is working fine (i am using 5.1.2, i have not tested on 5.1.3). The issue of this PHP Warning and Error Code 11 occurs both on Swoole and OpenSwoole (i tested on both).

  2. In order to avoid disconnection, we need to put the function recv() which contains the command $this->socket->recv(); inside a while(true) { ... } loop (in its consumer side code).

  3. In order to ignore / suppress the PHP Warning on your TCP-Client code use the sign @ just before $this->socket->recv(); to make it as @$this->socket->recv();
    Note: This PHP warning which comes only when there is no data pushed in socket.

here $this->socket is $this->socket = Swoole\Client(SWOOLE_SOCK_TCP); // may be different variable in your code

  1. Now from server-side if you send the data using push() repeatedly (like, by puting push() inside a loop), the client-side will show the data. As a test, i called push() twice with a co:sleep(10) in the middle.

FYI.

@NathanFreeman @matyhtf @twose

from swoole-src.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.