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

发布于 2024-11-17 12:15:50 字数 1633 浏览 3 评论 0原文

我将我的 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 握手方法规范中给出的示例来尝试此代码,并且可以正常使用。

im paging my php websocket server to node.js but i don't understand, why my handshake fails! Here is my code:

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

If someone has an idea, I would be amused :) Tryed this code with example given in v76 spec of handshake method and its ok there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-11-24 12:15:50

您可以参考 phpwebsocket 了解更多信息。

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

You can refer phpwebsocket for more information.

And in the latest WebSocket protocol version [draft-ietf-hybi-thewebsocketprotocol-09, the handshake mechanism has been updated, there will be only one "Sec-WebSocket-Key", you can learn more about the revised handshake mechanism from my article: http://www.codeproject.com/KB/HTML/Web-Socket-in-Essence.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文