I'm handling a websocket 'upgrade' event from a Node.js http server - The upgrade handler is in the form function(req, socket, head) - How can I send a response to this upgrade request given that there is no res object? Is there a way to do it using the socket object? How to send back headers?
Asked
Active
Viewed 5,228 times
9
-
Why do you use the http server for websockets? There are [plenty of dedicated libraries](http://stackoverflow.com/q/16392260). And yes, you're supposed to answer via the socket. – Bergi Aug 04 '13 at 18:31
-
I'm writing a custom proxy for engine.io. The node-http-proxy library (by Noejitsu) does not appear to work with engine.io - I just want to get the basic logic working at this stage. – Jon Aug 04 '13 at 21:46
1 Answers
8
You just need to call socket.write
with the appropriate HTTP syntax as plain text along these lines (from wikipedia):
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
Use \r\n
line separators. After that point, HTTP is over and now you are just using the bare TCP socket.

Peter Lyons
- 142,938
- 30
- 279
- 274