为WebSocket创建一个PHP套接字服务器[handshake]

时间:2014-11-19 22:24:55

标签: javascript php html5 sockets websocket

我尝试创建自己的PHP套接字服务器以使用HTML5 WebSocket API,但我无法执行握手步骤,我在这里阅读rfc6455我的PHP服务器代码:

这里是Javascript代码:

var socket = new WebSocket("ws://127.0.0.1:1577/server.php");

socket.onopen = function (e) {
    console.log("openned : "+e.readyState);
}
socket.onclose = function (e) {
    console.log("Socket connection closed : "+e);
}

这里是PHP代码:

<?php

set_time_limit(0);
$adr = "127.0.0.1";
$port = 1577;

$m_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$msg = "Welcome...";
$cls = array();

socket_bind($m_sock, $adr, $port);
socket_listen($m_sock, 5);
echo "Server start...\n\n";

do{
        $msgsock = socket_accept($m_sock);

        array_push($cls, $msgsock);
        echo "Connected...\n\n";
        usleep(100);
        //socket_write($msgsock, $msg, strlen($msg)); //this is the 'bug'
        do{
                if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
                   echo "socket_read() fail :" . socket_strerror(socket_last_error($msgsock)) . "\n";
                    break 2;
                }
                if(preg_match("/Sec-WebSocket-Key: (.*)/",$buf,$match)){ 
                        $key =  base64_encode(hash("sha1",trim($match[1])."258EAFA5-E914-47DA-95CA-C5AB0DC85B11")); 
                        $response_header =    "HTTP/1.1 101 Switching Protocols"."\r\n".
                        "Upgrade: websocket"."\r\n".
                        "Connection: Upgrade"."\r\n".
                        "Sec-WebSocket-Accept: $key"."\r\n\r\n";

                        //SERVER RESPONSE ----
                        socket_write($msgsock,$response_header,strlen($response_header));

                        echo "handshake done...";
                };
        } while(1);
        socket_close($msgsock);
} while(1);

我总是有这样的错误:失败:WebSocket握手期间出错:状态行无效 这里是HTTP请求:

HTTP请求:

Accept-Encoding:gzip, deflate, sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,he;q=0.2
Cache-Control:no-cache
Connection:Upgrade
Cookie:__utma=96992031.134451192.1399804258.1402844967.1402848436.4; __utmz=96992031.1399804258.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.4.134451192.1399804258; toolbarDisplay=hide
Host:127.0.0.1:1577
Origin:http://127.0.0.1
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:2yODScjZV/dap0DsDSWDFQ==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36

这里是PHP服务器生成的响应:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: MDQ1NjFhMzc1YjY5MWQwNTU1ZGIzNDYyZmM0YTc1ODFhMDBlMzdmOQ==

2 个答案:

答案 0 :(得分:1)

您可以阅读Ratchet的源代码,看看他们是如何完成的。

有一个类实现握手here

答案 1 :(得分:0)

只是通过查看响应,看起来你的websocket接受看起来不对,看起来它太长了。我知道这是因为我有完全相同的问题。如果这是我遇到的同样问题,那就是问题:

SHA-1传回十六进制值。这个值不能简单地转换为base64。必须首先将十六进制值转换为ascii文本,然后ascii文本值可以是base64编码。

这里的简单答案是简单地从SHA-1传回十六进制值并将其分成2个字符的块。取这2个字符,将它们转换为正常的十进制数字,然后查看数字对应的ascii字符代码。