事件:'session'
【Event: 'session'】
session<Buffer>
当客户端的 tls.TLSSocket 有新的会话或 TLS 票证可用时,会触发 'session' 事件。根据协商的 TLS 协议版本,这可能在握手完成之前或之后发生。该事件不会在服务器上触发,也不会在未创建新会话时触发,例如,在连接恢复时。对于某些 TLS 协议版本,该事件可能会触发多次,这种情况下所有会话都可以用于恢复。
【The 'session' event is emitted on a client tls.TLSSocket when a new session
or TLS ticket is available. This may or may not be before the handshake is
complete, depending on the TLS protocol version that was negotiated. The event
is not emitted on the server, or if a new session was not created, for example,
when the connection was resumed. For some TLS protocol versions the event may be
emitted multiple times, in which case all the sessions can be used for
resumption.】
在客户端,session 可以提供给 tls.connect() 的 session 选项以恢复连接。
【On the client, the session can be provided to the session option of
tls.connect() to resume the connection.】
有关更多信息,请参见会话恢复。
【See Session Resumption for more information.】
对于 TLSv1.2 及以下版本,tls.TLSSocket.getSession() 可以在握手完成后调用一次。对于 TLSv1.3,协议只允许基于票据的会话恢复,会发送多个票据,并且票据要等到握手完成后才发送。因此,有必要等待 'session' 事件来获得可恢复的会话。应用应使用 'session' 事件而不是 getSession(),以确保它们适用于所有 TLS 版本。只期望获取或使用一个会话的应用,应只监听此事件一次:
【For TLSv1.2 and below, tls.TLSSocket.getSession() can be called once
the handshake is complete. For TLSv1.3, only ticket-based resumption is allowed
by the protocol, multiple tickets are sent, and the tickets aren't sent until
after the handshake completes. So it is necessary to wait for the
'session' event to get a resumable session. Applications
should use the 'session' event instead of getSession() to ensure
they will work for all TLS versions. Applications that only expect to
get or use one session should listen for this event only once:】
tlsSocket.once('session', (session) => {
// The session can be used immediately or later.
tls.connect({
session: session,
// Other connect options...
});
});