我正在尝试在nodejs中编写一个非常简单的websocket服务器,但我遇到了问题。在浏览器上,只有WebSocket.onclose函数被触发(onopen、onmessage和onerror没有被触发)。我已经在Chrome7和FireFox4中进行了测试。以下是我的服务器代码:
var http = require('http'), net = require('net'), crypto = require('crypto');我的客户端代码:
var server = http.createServer(function (req, res) { console.log(req); });
server.on('connection', function (stream) { stream.setEncoding('utf8'); stream.setTimeout(0); stream.setNoDelay(true); stream.on('data', function (data) { var sec1_regex = /Sec-WebSocket-Key1:(.*)/g; var sec2_regex = /Sec-WebSocket-Key2:(.*)/g; var origin_regex = /Origin: (.*)/g; var protocol_regex = /Sec-WebSocket-Protocol: (.*)/g;
console.log(stream); console.log("****Incoming****\r\n" + data); var key1 = sec1_regex.exec(data)[1]; var num1 = parseInt(key1.match(/\d/g).join(''))/(key1.match(/\s/g).length - 1); console.log("num1: " + num1); var key2 = sec2_regex.exec(data)[1]; var num2 = parseInt(key2.match(/\d/g).join(''))/(key2.match(/\s/g).length - 1); console.log("num2: " + num2); var lastbytes = data.slice(-8); var origin = origin_regex.exec(data)[1];
var md5 = crypto.createHash('md5'); md5.update(String.fromCharCode(num1 >> 24 & 0xFF, num1 >> 16 & 0xFF, num1 >> 8 & 0xFF, num1 & 0xFF)); md5.update(String.fromCharCode(num2 >> 24 & 0xFF, num2 >> 16 & 0xFF, num2 >> 8 & 0xFF, num2 & 0xFF)); md5.update(lastbytes); var response = "HTTP/1.1 101 WebSocket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Origin: " + origin + "\r\nSec-WebSocket-Location: ws://127.0.0.1:8124/\r\n" + md5.digest('binary'); stream.write(response, 'binary'); console.log("****Outgoing****\r\n" + response); }); });
server.listen(8124, '127.0.0.1');
function connect() { if (window.WebSocket) { try { // 创建一个WebSocket对象,连接到指定的服务器 ws = new WebSocket('ws://127.0.0.1:8124'); // 当WebSocket打开时执行以下操作 ws.onopen = function () { alert("连接已打开"); }; // 当WebSocket关闭时执行以下操作 ws.onclose = function() { alert("连接已关闭"); }; // 当WebSocket发生错误时执行以下操作 ws.onerror = function(err) { alert("发生错误!"); }; // 当WebSocket接收到消息时执行以下操作 ws.onmessage = function() { alert('收到消息'); }; } catch (ex) { alert(ex); } } }
parseInt(..., 10)
来确保将字符串解析为十进制数字。否则,你可能会得到奇怪的结果:parseInt("070") === 56
。 - pimvdb