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.