varspool / Wrench

A simple PHP WebSocket implementation for PHP 7.1
Do What The F*ck You Want To Public License
596 stars 210 forks source link

ssl only: Error reading data from socket: Not connected #80

Closed dumshi closed 7 years ago

dumshi commented 8 years ago

Chrome gives this error: WebSocket connection to 'wss://server.somewhere:8000/echo' failed: WebSocket opening handshake was canceled

I get this error when testing server_ssl.php in cli:

root@m1048:/var/www/html/Wrench-master/examples# php -q server_ssl.php info: Server initialized info: Wrench\ConnectionManager: Wrench\Connection: 80.xxx.xxx.xxx:61207 (38bcbf66a45efe6f76292087dacb1b3fdbb35262dcce1d98c5473f0d4ba62794e0126f634cc264c519727021553be8b420fcb795f3f53eb6aa82cc7d7725a7c2): Connected notice: Wrench\ConnectionManager: Client connection closed: exception 'Wrench\Exception\CloseException' with message 'Error reading data from socket: Not connected' in /var/www/html/Wrench-master/lib/Wrench/Connection.php:396 Stack trace:

0 /var/www/html/Wrench-master/lib/Wrench/ConnectionManager.php(243): Wrench\Connection->process()

1 /var/www/html/Wrench-master/lib/Wrench/ConnectionManager.php(168): Wrench\ConnectionManager->processClientSocket(Resource id #32)

2 /var/www/html/Wrench-master/lib/Wrench/Server.php(198): Wrench\ConnectionManager->selectAndProcess()

3 /var/www/html/Wrench-master/examples/server_ssl.php(54): Wrench\Server->run()

4 {main}

warning: Wrench\ConnectionManager: Wrench\Connection: 80.xxx.xxx.xxx:61207 (38bcbf66a45efe6f76292087dacb1b3fdbb35262dcce1d98c5473f0d4ba62794e0126f634cc264c519727021553be8b420fcb795f3f53eb6aa82cc7d7725a7c2): Unable to send close message

This does not happen in normal plain "server.php" test, only in ssl.

dumshi commented 8 years ago

Ok, After few testing and reading. I also went to https://server.somewhere:8000 port and accepted the self-signed cert. I have to leave that window OPEN for the next window with https://server.somewhere/ with wss:// to work.

Also started to notice weird things when I send a chat msg.

WebSocket - status 0 Welcome - status 1 Sent: asdf Sent: asdf Received: asdf Sent: asdfasdf Received: asdf Sent: a Received: asdfasdf Sent: a Received: a Sent: a Received: a Sent: asdf Received: a Sent: a Received: asdf Sent: asdf Received: a

Notice how the first msg was never "sent" (hence did not receive immediately). Asap, i sent the 2nd msg, the first msg bounced back (assuming when i sent the 2nd msg, the first was went to server). Is there a Queue system of some kind in SSL mode ??

What is going on?

I had same problem with my other project, and so I decided to download a fresh new Wrench copy. And I am seeing similar things.

Please someone explain.

Help!!!

dumshi commented 8 years ago

I am using helper for the workaround. If anyone knows how to fix this, please let me know. Thanks in advance.

Workaround: WebSocket helpers * Checks websocket state before sending. / this.waitForConnection = function (callback, interval) { if (websocket.readyState === 1) { callback(); } else { var that = this; setTimeout(function () { that.waitForConnection(callback); }, interval); } }; function send(message, callback) { this.waitForConnection(function () {

        websocket.send(message);
            websocket.send(""); //send empty msg after your real msg.

        if (typeof callback !== 'undefined') {
          callback();
        }
    }, 1000);
};
Alarmfifa commented 8 years ago

Hi! I'm not sure that this problem is related to server hang, but could you check this fix

emadruida commented 8 years ago

It looks like Issue #61 to me. See if this can help you.

dumshi commented 8 years ago

@Alarmfifa It did not look like anything to do with Large Messages, as I was only send "asdf". But thank you for your reply.

@emadruida Your fix did the trick.

Thank you both for quick reply.

Alarmfifa commented 8 years ago

@Alarmfifa It did not look like anything to do with Large Messages, as I was only send "asdf". But thank you for your reply.

"Large message" isn't only for message data. It is about full http message with it's header (cookies for example). I debugged with small message, but with a big header in one browser. So, my problem is a bit difficult for reproducing.

dominics commented 7 years ago

The fix mentioned by @emadruida has been merged into v2.0.8 (and v3.0.x-dev) (9dd8175) - let me know if that fixes things