2011-11-20 29 views

回答

0

要送你需要握手按照定义实施算法:

// request is a string containing the handshake request 

var match = /^Sec-WebSocket-Key: (.*)$/.exec(request); // parse request key 

if(!match) { 
    // not a valid request 
} 

var crypto = require("crypto"), 
    hash = crypto.createHash("sha1"), 
    add = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 

hash.update(match[1] + add); 

var key = hash.digest("base64"); // get response key 

// build response 
var response = "HTTP/1.1 101 Switching Protocols\r\n" + 
       "Upgrade: websocket\r\n" + 
       "Connection: Upgrade\r\n" + 
       "Sec-WebSocket-Accept: " + key + "\r\n\r\n"; 

// send response now 

要发送和接收消息,请参阅this wiki question

+0

我应该在哪里放这段代码? – Uw001

+0

在'handshake'函数中,以'data'作为请求字符串,并使用'socket.write'写入响应。 – pimvdb