Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple new Connection initiates #17

Open
clarkk opened this issue Aug 30, 2018 · 7 comments
Open

Multiple new Connection initiates #17

clarkk opened this issue Aug 30, 2018 · 7 comments

Comments

@clarkk
Copy link

clarkk commented Aug 30, 2018

I can see you have more than one new Connection

For better performance I would recommend only doing it once but then store all connections in an array like you store all clients as a property in the class

167            $this->handler->onOpen(new Connection($newClient));
...
189            $this->cureentConn = new Connection($sock);
@ROTGP
Copy link

ROTGP commented Aug 13, 2019

Agree. I connect with the following code...

$client = new WebSocketClient('wss://...', $config);

And the socket server registers two connections. Is there any workaround for this, @arthurkushman ?

@arthurkushman arthurkushman pinned this issue Aug 13, 2019
@arthurkushman
Copy link
Owner

Hi, could You provide more info on this? How do run client, let's assume that the server is running commonly without any additional configuration, in other words I need 100% client code to reproduce and explore. Thx

@ROTGP
Copy link

ROTGP commented Aug 13, 2019

Sure, I'm testing in localhost, so I'm disabling SSL verification. Full code is as follows:

$config = new ClientConfig();
$config->setContextOptions(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
$client = new WebSocketClient('wss://domain.com:6001', $config);

I've also tried send/receive on the aforementioned $client and it works as expected (albeit twice)... the server receives the sent message and returns a response, which the client receives without a problem. It's just that it happens twice.

The server is quite standard. With the above code it logs two separate connections. When I connect to the same server with the the following javascript code, the server only receives a single connection (as hoped/expected)

new WebSocket("wss://domain.com:6001")

I have also tried connecting to the server with Flutter (a cross-platform mobile framework) and it behaves exactly the same as the javascript example (single connection).

I appreciate this is difficult to debug, are the comments from the original bug not helpful?

@arthurkushman
Copy link
Owner

I'm running the web-socket server in one tab and client in another with this code:

        $recvMsg = '{"user_id" : 123}';
        $client = new WebSocketClient($this->url, new ClientConfig());
        try {
            $client->send($recvMsg);
        } catch (BadOpcodeException $e) {
            echo 'Couldn`t sent: ' . $e->getMessage();
        }
        $recv = $client->receive();
        $this->assertEquals($recv, $recvMsg);

Getting this particular result:

[2019-08-13 15:42:01] ServerSocket.DEBUG: Connection opened, total clients: 1 [] []
[2019-08-13 15:42:01] ServerSocket.DEBUG: Array (     [1090] => WSSC\Components\Connection Object         (             [socketConnection:WSSC\Components\Connection:private] => Resource id #1090             [clients:WSSC\Components\Connection:private] => Array                 (                     [9] => Resource id #1090                 )          )  )  [] []
[2019-08-13 15:42:01] ServerSocket.DEBUG: Received message:  {"user_id" : 123} [] []
[2019-08-13 15:42:01] ServerSocket.DEBUG: close: Array ( )  [] []

Probably there some miscellaneous connections in your case from 2 clients ex. browser and console?

@ROTGP
Copy link

ROTGP commented Aug 13, 2019

It's a mystery. The server I'm running is this one, which is based on Ratchet. The 2 connections are definitely a result of the php client connecting (a single time). I'm pretty new to socket programming... I wonder if the way that the php client connects is interpreted by the server as two separate connections?

@arthurkushman
Copy link
Owner

It is a mystery for me either, because I have never used laravel web-socker server, but for my own interest I've installed it and run with

php artisan websockets:serve
Starting the WebSocket server on port 6001...

It is returning

HTTP/1.1 404 Not Found
X-Powered-By: Ratchet/0.4.1

I don't know why and I don't want to spend my time investigating it to be honest.
PS with wss web-socket server it works particularly as it should with 1 connection per client.
Also it was strange to mention via netstat -a -n | grep 6001 that there are 2 instance of server running, probably they try to "cluster" something.

@ROTGP
Copy link

ROTGP commented Aug 14, 2019

Thanks anyway - I appreciate your efforts. I'll let you know if I ever find out the cause.

@arthurkushman arthurkushman unpinned this issue Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants