2010google code的websocket代码与最新的websocket标准有出入

来源:互联网 发布:js 比较对象 编辑:程序博客网 时间:2024/04/28 22:38

代码地址:http://code.google.com/p/phpwebsocket/

其中server.php和websocket.class.php中的代码和chrome版本 28.0.1500.72的websocket客户端连接异常

其中需要修改的代码为:

原始代码1:

  function dohandshake($user,$buffer){    $this->log("\nRequesting handshake...");    $this->log($buffer);    list($resource,$host,$origin,$key1,$key2,$l8b) = $this->getheaders($buffer);    $this->log("Handshaking...");    //$port = explode(":",$host);    //$port = $port[1];    //$this->log($origin."\r\n".$host);    $upgrade  = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" .                "Upgrade: WebSocket\r\n" .                "Connection: Upgrade\r\n" .                                //"WebSocket-Origin: " . $origin . "\r\n" .                                //"WebSocket-Location: ws://" . $host . $resource . "\r\n" .                "Sec-WebSocket-Origin: " . $origin . "\r\n" .                    "Sec-WebSocket-Location: ws://" . $host . $resource . "\r\n" .                    //"Sec-WebSocket-Protocol: icbmgame\r\n" . //Client doesn't send this                "\r\n" .                    $this->calcKey($key1,$key2,$l8b) . "\r\n";// .                        //"\r\n";    socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));    $user->handshake=true;    $this->log($upgrade);    $this->log("Done handshaking...");    return true;  }
  function getheaders($req){    $r=$h=$o=null;    if(preg_match("/GET (.*) HTTP/"               ,$req,$match)){ $r=$match[1]; }    if(preg_match("/Host: (.*)\r\n/"              ,$req,$match)){ $h=$match[1]; }    if(preg_match("/Origin: (.*)\r\n/"            ,$req,$match)){ $o=$match[1]; }    if(preg_match("/Sec-WebSocket-Key1: (.*)\r\n/",$req,$match)){ $this->log("Sec Key1: ".$sk1=$match[1]); }    if(preg_match("/Sec-WebSocket-Key2: (.*)\r\n/",$req,$match)){ $this->log("Sec Key2: ".$sk2=$match[1]); }    if($match=substr($req,-8))                                                                  { $this->log("Last 8 bytes: ".$l8b=$match); }    return array($r,$h,$o,$sk1,$sk2,$l8b);  }


修改后的代码1:

  function dohandshake($user,$buffer){    $this->log("\nRequesting handshake...");    $this->log($buffer);    list($resource,$host,$origin,$key1,$key2,$l8b) = $this->getheaders($buffer);    $this->log("Handshaking...");    //$port = explode(":",$host);    //$port = $port[1];    //$this->log($origin."\r\n".$host);    $upgrade  = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" .                "Upgrade: WebSocket\r\n" .                "Connection: Upgrade\r\n" .                "Sec-WebSocket-Accept:" . $this->calcKey($key1,$key2,$l8b) . "\r\n" .                                //"WebSocket-Origin: " . $origin . "\r\n" .                                //"WebSocket-Location: ws://" . $host . $resource . "\r\n" .                "Sec-WebSocket-Origin: " . $origin . "\r\n" .                "Sec-WebSocket-Location: ws://" . $host . $resource . "\r\n" .                    //"Sec-WebSocket-Protocol: icbmgame\r\n" . //Client doesn't send this                "\r\n" .//                    $this->calcKey($key1,$key2,$l8b) . "\r\n";// .                "\r\n";    socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));    $user->handshake=true;    $this->log($upgrade);    $this->log("Done handshaking...");    return true;  }

  function getheaders($req){    $r=$h=$o=null;    if(preg_match("/GET (.*) HTTP/"               ,$req,$match)){ $r=$match[1]; }    if(preg_match("/Host: (.*)\r\n/"              ,$req,$match)){ $h=$match[1]; }    if(preg_match("/Origin: (.*)\r\n/"            ,$req,$match)){ $o=$match[1]; }    if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)){ $this->log("Sec Key1: ".$sk1=$match[1]); }    if(preg_match("/Sec-WebSocket-Key2: (.*)\r\n/",$req,$match)){ $this->log("Sec Key2: ".$sk2=$match[1]); }    if($match=substr($req,-8))                                                                  { $this->log("Last 8 bytes: ".$l8b=$match); }    return array($r,$h,$o,$sk1,$sk2,$l8b);  }


原始代码2:

  function calcKey($key1,$key2,$l8b){        //Get the numbers        preg_match_all('/([\d]+)/', $key1, $key1_num);        preg_match_all('/([\d]+)/', $key2, $key2_num);        //Number crunching [/bad pun]        $this->log("Key1: " . $key1_num = implode($key1_num[0]) );        $this->log("Key2: " . $key2_num = implode($key2_num[0]) );        //Count spaces        preg_match_all('/([ ]+)/', $key1, $key1_spc);        preg_match_all('/([ ]+)/', $key2, $key2_spc);        //How many spaces did it find?        $this->log("Key1 Spaces: " . $key1_spc = strlen(implode($key1_spc[0])) );        $this->log("Key2 Spaces: " . $key2_spc = strlen(implode($key2_spc[0])) );        if($key1_spc==0|$key2_spc==0){ $this->log("Invalid key");return; }        //Some math        $key1_sec = pack("N",$key1_num / $key1_spc); //Get the 32bit secret key, minus the other thing        $key2_sec = pack("N",$key2_num / $key2_spc);        //This needs checking, I'm not completely sure it should be a binary string        return md5($key1_sec.$key2_sec.$l8b,1); //The result, I think  }
修改后的代码2:

function calcKey($key1,$key2,$l8b){$key = $key1;$key = $key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';$key = sha1($key);   $len = strlen($key);$str = '';for ($i = 0; $i < $len; $i+=2){    $str .= chr(hexdec($key[$i].$key[$i+1]));}return base64_encode($str);}
参考http://bug1874.com/archives/225


最终发现这个版本的php server代码与最新的websocket协议不一致,因此无法使用。。。

原创粉丝点击