Quoted from TCP keepalive HowTo:Tại sao NodeJS KeepAlive dường như không hoạt động như mong đợi?
In order to understand what TCP keepalive (which we will just call keepalive) does, you need do nothing more than read the name: keep TCP alive. This means that you will be able to check your connected socket (also known as TCP sockets), and determine whether the connection is still up and running or if it has broken.
Vậy tại sao là mã sau không ném một cái gì đó khi kết nối Internet bị hỏng?
var tls = require('tls');
var socket = tls.connect(443, "google.com", function connected() {
console.log('connected');
});
socket.setNoDelay(true);
socket.setKeepAlive(true, 0);
socket.setTimeout(0, function(){
console.log('timeout');
});
socket.on('data', function(data) {
console.log(data);
});
socket.on('close', function() {
console.error("close");
});
socket.on('error', function(err) {
console.error("error", err);
});
Thử nghiệm trên hệ điều hành MacOS/Debian, với NodeJS v0.10.17