Node.js如何响应升级请求? [英] Node.js how to respond to an upgrade request?

查看:129
本文介绍了Node.js如何响应升级请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆