2
我試圖構建管理與node.js的WebSocket連接的WebSocket握手
我沒有找到一種方法,有握手是接受鍍鉻
鉻返回代碼「的WebSocket WebSocket的握手過程中出錯:連接 'WS://127.0.0.1:8888 /' 失敗二段的WebSocket,接受不匹配「
與此
< < >>>
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");
});
< < < >>>>
希望有人會看到什麼缺失。