0

我在 PHP 中学习 websocket,但我不知道为什么会有这个握手错误。

我的 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>

和 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);

但是,我遵循了关于握手的 Mozilla 文档:/

谢谢你。

4

2 回答 2

0

但是,我遵循了关于握手的 Mozilla 文档:/

虽然这可能是您根据您的代码假设只需要一开始的握手并且您可以直接使用普通套接字。但是 WebSockets 实际上有自己的消息结构和有效负载屏蔽,你也需要实现这些。详见实际标准

于 2020-03-28T23:45:59.847 回答
0

您的要求仍然适用吗?

我发送:

$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";

我最终得到 2 \n\n\r。

这部分不能工作:

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

你必须尊重框架

 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 ...                |
+---------------------------------------------------------------+
于 2022-02-13T15:02:00.503 回答