'resumeSession' 事件


当客户端请求恢复之前的 TLS 会话时,则会触发 'resumeSession' 事件。 监听器回调在调用时传入两个参数:

  • sessionId <Buffer> TLS 会话标识符
  • callback <Function> 恢复前一个会话时要调用的回调函数:callback([err[, sessionData]])

事件监听器应该使用给定的 sessionId 在外部存储中为 'newSession' 事件处理程序保存的 sessionData 执行查找。 如果找到,则调用 callback(null, sessionData) 恢复会话。 如果没有找到,则会话将无法恢复。 callback() 必须在没有 sessionData 的情况下被调用,以便握手可以继续并可以创建新的会话。 可以调用 callback(err) 来终止传入的连接并销毁套接字。

监听此事件只会对添加事件监听器后建立的连接有影响。

以下说明恢复 TLS 会话:

const tlsSessionStore = {};
server.on('newSession', (id, data, cb) => {
  tlsSessionStore[id.toString('hex')] = data;
  cb();
});
server.on('resumeSession', (id, cb) => {
  cb(null, tlsSessionStore[id.toString('hex')] || null);
});

The 'resumeSession' event is emitted when the client requests to resume a previous TLS session. The listener callback is passed two arguments when called:

  • sessionId <Buffer> The TLS session identifier
  • callback <Function> A callback function to be called when the prior session has been recovered: callback([err[, sessionData]])

The event listener should perform a lookup in external storage for the sessionData saved by the 'newSession' event handler using the given sessionId. If found, call callback(null, sessionData) to resume the session. If not found, the session cannot be resumed. callback() must be called without sessionData so that the handshake can continue and a new session can be created. It is possible to call callback(err) to terminate the incoming connection and destroy the socket.

Listening for this event will have an effect only on connections established after the addition of the event listener.

The following illustrates resuming a TLS session:

const tlsSessionStore = {};
server.on('newSession', (id, data, cb) => {
  tlsSessionStore[id.toString('hex')] = data;
  cb();
});
server.on('resumeSession', (id, cb) => {
  cb(null, tlsSessionStore[id.toString('hex')] || null);
});