我正在尝试构建一个管理与 node.js 的 websocket 连接的代码
我没有找到让握手在 chrome 中被接受的方法
Chrome 返回“与 'ws://127.0.0.1:8888/' 的 WebSocket 连接失败:WebSocket 握手期间出错:Sec-WebSocket-Accept 不匹配”
有了这个
<<>>>
var http=require("http");
var crypto=require("crypto");
var server=http.createServer(function(req,res){
res.writeHeads("200","content-type:text/plain");
res.end();
});
server.on("upgrade",function(req,socket,head){
for(var item in req.headers){
console.log(item);
console.log(req.headers[item]);
}
var GUID="258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
var secKey=req.headers["sec-websocket-key"];
var sha=crypto.createHash("sha1");
secKey+=GUID;
secKey=sha.update(secKey).digest("base64");
var key64=new Buffer(secKey);
key64=key64.toString("base64");
console.log(key64);
var headersReturn='HTTP/1.1 101 Switching Protocols\r\n';
headersReturn+='Upgrade: websocket\r\n';
headersReturn+='Connection: Upgrade\r\n';
headersReturn+='Sec-WebSocket-Accept:'+key64+"\r\n\r\n";
var boo=socket.write(headersReturn);
console.log(headersReturn);
socket.on("connect",function(){console.log("success");});
socket.on("data",function(data){
console.log(data);
});
});
server.listen(8888,"127.0.0.1",function(){
console.log("server is on 8888");
});
<<<>>>>
希望有人能看到缺少的东西。