Node.js如何响应升级请求? [英] Node.js how to respond to an upgrade request?
问题描述
我正在从Node.js http服务器处理websocket'upgrade'事件 - 升级处理程序在表单函数(req,socket,head)中 - 如何在此处发送响应此升级请求没有res对象?有没有办法使用套接字对象?如何发回标题?
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?
推荐答案
你只需要调用 socket.write
使用适当的HTTP语法作为这些行的纯文本(来自维基百科):
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
使用 \ r \ n
行分隔符。在那之后,HTTP结束了,现在你只是使用裸TCP套接字。
Use \r\n
line separators. After that point, HTTP is over and now you are just using the bare TCP socket.
这篇关于Node.js如何响应升级请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!