admin管理员组

文章数量:1277338

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?

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?

Share Improve this question asked Aug 4, 2013 at 17:02 JonJon 1,3142 gold badges14 silver badges24 bronze badges 2
  • Why do you use the http server for websockets? There are plenty of dedicated libraries. And yes, you're supposed to answer via the socket. – Bergi Commented Aug 4, 2013 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 Commented Aug 4, 2013 at 21:46
Add a ment  | 

1 Answer 1

Reset to default 10

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.

本文标签: javascriptNodejs how to respond to an upgrade requestStack Overflow