PHP Websocket failed: Error during WebSocket handshake: net::ERR_CONNECTION_CLOSED (anonymous)

919 Views Asked by At

i learning the websocket in PHP but i don't know why have this handshake error.

My client.html :

<html>
<body>
    <div id="root"></div>
    <script>
        var host = 'ws://127.0.0.1:8020/';
        var socket = new WebSocket(host);
        socket.onmessage = function(e) {
            console.log(e.data)
        };
    </script>
</body>
</html>

And server.php :

<?php

$address = "127.0.0.1";
$port = 8020;

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,$address,$port) or die('bind error');
socket_listen($socket) or die('listen error');
$client = socket_accept($socket) or die('accept error');

$socketread = socket_read($client,5000) or die('Failed to read');

preg_match("#Sec-WebSocket-Key: (.*)\r\n#",$socketread,$match);
$key = base64_encode(sha1($match[1].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11',true));

$header = "HTTP/1.1 101 Switching Protocols\r\n";
$header .= "Upgrade: websocket\r\n";
$header .= "Connection: Upgrade\r\n";
$header .= "Sec-WebSocket-Accept: $key\r\n";
$header .= "Sec-WebSocket-Version: 13\r\n";

var_dump($header);
socket_write($client,$header,strlen($header));


$msg = "connected";
socket_write($client,$msg,strlen($msg));
socket_close($socket);

However, I followed the Mozilla documentation on the handshake :/

Thank you for that.

2

There are 2 best solutions below

5
On BEST ANSWER

However, I followed the Mozilla documentation on the handshake :/

While this might be you assume based on your code that only the handshake at the beginning is needed and that you can just use a plain socket directly. But WebSockets actually have their own message structure and also payload masking which you need to implement too. See the actual standard for the details.

0
On

Your request and still relevant ?

I send :

$key = base64_encode(sha1($match[1].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11',true));
$header = "HTTP/1.1 101 Switching Protocols\n\r"
                        ."Upgrade: websocket\n\r"
                        ."Connection: Upgrade\n\r"
                        ."Sec-WebSocket-Accept: ".base64_encode(sha1($cartouche['Sec-WebSocket-Key'].'258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true))."\n\n\r";

I end up with 2 \n\n\r.

this part can't work :

$msg = "connected";
socket_write($client,$msg,strlen($msg));
socket_close($socket);

You must respect the frame

 0               1               2               3
 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
|N|V|V|V|       |S|             |   (if payload len==126/127)   |
| |1|2|3|       |K|             |                               |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 4               5               6               7
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|     Extended payload length continued, if payload len == 127  |
+ - - - - - - - - - - - - - - - +-------------------------------+
 8               9               10              11
+ - - - - - - - - - - - - - - - +-------------------------------+
|                               |Masking-key, if MASK set to 1  |
+-------------------------------+-------------------------------+
 12              13              14              15
+-------------------------------+-------------------------------+
| Masking-key (continued)       |          Payload Data         |
+-------------------------------- - - - - - - - - - - - - - - - +
:                     Payload Data continued ...                :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                     Payload Data continued ...                |
+---------------------------------------------------------------+