0

I use this code in my project: https://github.com/ncr/node.ws.js/blob/master/ws.js

With Opera, Safari, and FF it works perfectly, but with googlechrome it doesn't works, because chrome use an other protocol (draft-ietf-hybi-thewebsocketprotocol-08)

I can't setup socket.io, so I have to modify this code: https://github.com/ncr/node.ws.js/blob/master/ws.js

Uw001
  • 363
  • 1
  • 5
  • 12

1 Answers1

0

To send the handshake you need to implement the algorithm as it is defined:

// 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

For sending and receiving messages please see this wiki question.

Community
  • 1
  • 1
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • In the `handshake` function, with `data` as the request string, and using `socket.write` to write the response. – pimvdb Nov 21 '11 at 16:04