事件:'upgrade'


🌐 Event: 'upgrade'

每当客户端的 HTTP 升级请求被接受时都会触发。默认情况下,所有 HTTP 升级请求都会被忽略(即只会触发常规的 'request' 事件,遵循正常的 HTTP 请求/响应流程),除非你监听此事件,在这种情况下,所有请求都会被接受(即触发 'upgrade' 事件,且未来的通信必须直接通过原始流处理)。你可以通过使用服务器的 shouldUpgradeCallback 选项更精确地控制这一行为。

🌐 Emitted each time a client's HTTP upgrade request is accepted. By default all HTTP upgrade requests are ignored (i.e. only regular 'request' events are emitted, sticking with the normal HTTP request/response flow) unless you listen to this event, in which case they are all accepted (i.e. the 'upgrade' event is emitted instead, and future communication must handled directly through the raw stream). You can control this more precisely by using the server shouldUpgradeCallback option.

参加此活动是可选的,客户不能要求更改协议。

🌐 Listening to this event is optional and clients cannot insist on a protocol change.

如果 shouldUpgradeCallback 接受了升级,但没有注册事件处理程序,则套接字将被销毁,从而导致客户端的连接立即关闭。

🌐 If an upgrade is accepted by shouldUpgradeCallback but no event handler is registered then the socket will be destroyed, resulting in an immediate connection closure for the client.

在罕见的情况下,如果传入请求有请求体,该请求体将按正常方式进行解析,与升级流分开,并且原始流数据仅在请求体解析完成后才开始。为了确保从流中读取不会因为等待请求体被读取而阻塞,任何对流的读取都会自动开始请求体流动。如果你想读取请求体,请确保在开始读取升级流之前这样做(即你附加 'data' 监听器)。

🌐 In the uncommon case that the incoming request has a body, this body will be parsed as normal, separate to the upgrade stream, and the raw stream data will only begin after it has completed. To ensure that reading from the stream isn't blocked by waiting for the request body to be read, any reads on the stream will start the request body flowing automatically. If you want to read the request body, ensure that you do so (i.e. you attach 'data' listeners) before starting to read from the upgraded stream.

stream 参数通常是 request 使用的 <net.Socket> 实例,但在某些情况下(例如有请求体时)它可能是一个双工流。如果需要,你可以通过 request.socket 访问请求底层的原始连接,它保证是 <net.Socket> 的实例,除非用户指定了其他类型的 socket。

🌐 The stream argument will typically be the <net.Socket> instance used by the request, but in some cases (such as with a request body) it may be a duplex stream. If required, you can access the raw connection underlying the request via request.socket, which is guaranteed to be an instance of <net.Socket> unless the user specified another socket type.