node.js websocket握手draft-hixie-thewebsocketprotocol-76不起作用!

问题描述

| 即时通讯我的PHP websocket服务器到node.js,但我不明白,为什么我的握手失败!这是我的代码:
var msg = data;
//console.log(msg);

tmp = msg.match(/\\r\\n\\r\\n(.*?)$/);
if(tmp) code = tmp[1];

tmp = msg.match(/GET (.*?) HTTP\\/1.1/);
if(tmp) res = tmp[1];

tmp = msg.match(/Sec-WebSocket-Key1: (.*?)\\r\\n/);
if(tmp) key1 = tmp[1];

tmp = msg.match(/Sec-WebSocket-Key2: (.*?)\\r\\n/);
if(tmp) key2 = tmp[1];

tmp = msg.match(/Host: (.*?)\\r\\n/);
if(tmp) host = tmp[1];

tmp = msg.match(/Origin: (.*?)\\r\\n/);
if(tmp) origin = tmp[1];

tmp = msg.match(/Upgrade: (.*?)\\r\\n/);
if(tmp) upgrade = tmp[1];

tmp = msg.match(/Connection: (.*?)\\r\\n/);
if(tmp) connection = tmp[1];

tmp = key1.match(/[0-9]/g);
num1 = parseInt(tmp.join(\'\'));

tmp = key2.match(/[0-9]/g);
num2 = parseInt(tmp.join(\'\'));

tmp = key1.match(/\\s/g);
spa1 = tmp.length;

tmp = key2.match(/\\s/g);
spa2 = tmp.length;

part1 = num1/spa1;
part2 = num2/spa2;

var md5 = crypto.createHash(\'md5\');

test1 = String.fromCharCode(part1 >> 24 & 0xFF,part1 >> 16 & 0xFF,part1 >> 8 & 0xFF,part1 & 0xFF);
md5.update(test1);  

test2 = String.fromCharCode(part2 >> 24 & 0xFF,part2 >> 16 & 0xFF,part2 >> 8 & 0xFF,part2 & 0xFF);
md5.update(test2);

md5.update(code);

var fin = md5.digest(\'binary\');

var out = \"HTTP/1.1 101 Web Socket Protocol Handshake\\r\\n\"+\'Upgrade: \'+upgrade+\"\\r\\n\"+\'Connection: \'+connection+\"\\r\\n\"+\'Sec-WebSocket-Origin: \'+origin+\"\\r\\n\"+\'Sec-WebSocket-Location: ws://\'+host+res+\"\\r\\n\\r\\n\"+fin;

console.log(out);
socket.write(out,\'utf8\');
如果有人有一个主意,我会被逗乐:)尝试使用v76 spec握手方法规范中给出的示例并在这里确定该代码。     

解决方法

        您可以参考phpwebsocket以获得更多信息。 并且在最新的WebSocket协议版本[draft-ietf-hybi-thewebsocketprotocol-09中,握手机制已更新,只有一个“ Sec-WebSocket-Key”,您可以从中了解有关修订后的握手机制的更多信息我的文章:http://www.codeproject.com/KB/HTML/Web-Socket-in-Essence.aspx     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...