socket.setTimeout(timeout[, callback])


设置套接字在套接字上空闲 timeout 毫秒后超时。默认情况下,net.Socket 没有超时。

【Sets the socket to timeout after timeout milliseconds of inactivity on the socket. By default net.Socket do not have a timeout.】

当空闲超时被触发时,套接字将收到 'timeout' 事件,但连接不会被断开。用户必须手动调用 socket.end()socket.destroy() 来结束连接。

【When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed. The user must manually call socket.end() or socket.destroy() to end the connection.】

socket.setTimeout(3000);
socket.on('timeout', () => {
  console.log('socket timeout');
  socket.end();
}); 

如果 timeout 为 0,则现有的空闲超时将被禁用。

【If timeout is 0, then the existing idle timeout is disabled.】

可选的 callback 参数将作为 'timeout' 事件的单次监听器添加。

【The optional callback parameter will be added as a one-time listener for the 'timeout' event.】