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); }
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协议不一致,因此无法使用。。。
- 2010google code的websocket代码与最新的websocket标准有出入
- websocket客户端的代码
- 初识标准H5的WebSocket
- HTML5的威胁:WebSocket与稳定的标准
- 转侯捷关于VC6与标准的出入
- websocket最新协议的握手实现
- WebSocket的过程与理解
- Spring与WebSocket的整合
- WebSocket的特点与应用
- WebSocket的原理与优缺点
- websocket的介绍与使用
- WebSocket(4)-- WebSocket与TCP、Http的关系
- WebSocket(4)-- WebSocket与TCP、Http的关系
- WebSocket(4)-- WebSocket与TCP、Http的关系
- Cocos2d-x-javaScript 的webSocket的代码
- Cocos2d-x-javaScript 的webSocket的代码
- 第一次成功实现的WebSocket代码
- WebSocket WebSocket的简单用法 WebSocket的使用
- 最长递增子序列-Longest Increasing Subsequence
- maven构建spring mvc项目 + Mybatis整合
- web页面之切换输入域组
- android启动时,如何执行shell脚本
- 使用DBMS_SQL包
- 2010google code的websocket代码与最新的websocket标准有出入
- POJ 1664 放苹果
- [StructLayout(LayoutKind.Sequential) ]是结构体排序问题;
- json和map的相互转换
- Android学习历程-从菜鸟开始
- hdu 1517 A Multiplication Game
- grep
- C++的杂七杂八:我家的返回值才不可能这么傲娇(右值引用和移动语义)
- 学习存储过程