I am communicating with a machine using net.Socket
via TCP/IP.
I am able to establish the connection and to send and receive packets of Buffers, which is all fine.
The problem is that if I manually disconnect the internet cable from the machine, my Node.js connection doesn't fire the close
event and I have no means to know if something failed!
let socket;
const start = async function ( config ) {
await connecToBarrier( config )
.then( () => console.log( "connected to barrrier" ) )
.catch( err => {
throw new Error( `Cannot connect to barrier: ${err}` );
} );
};
const connecToBarrier = function ( connectOpts ) {
return new Promise( ( resolve, reject ) => {
//connectOpts is an object with 'port' and 'host'
socket = net.createConnection( connectOpts, () => {
//once I receive a 'hello world' from the machine, I can continue
socket.once( "data", () => resolve() );
} );
socket.on( "connection", () => {
console.log("onConnection says we have someone!");
} );
socket.on( "error", err => {
console.log(`onError says: ${err}`);
reject(err);
} );
socket.on( "timeout", () => {
console.log("onTimeout says nothing");
reject();
} );
socket.on( "end", () => {
console.log("onEnd says nothing");
reject();
} );
socket.on( "close", err => {
console.log(`onClose says: ${err}`);
reject(err);
} );
} );
};
start();
@robertklep mentioned the setKeepAlive
option, however according to
How to test socket.setKeepAlive in NodeJS
it doesn't work. A more profound research shows that this is highly dependant on the Operative System you are using, as per https://stackoverflow.com/a/18678494/1337392
So in other words, unless I am willing to wait several minutes for my heartbeats to actually do something, I don't see a way out of this.
How do I detect if a connection died?
If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
You can check the socket. connected property: var socket = io. connect(); console.
What is allowHalfOpen? allowHalfOpen {boolean} Indicates whether half-opened TCP connections are allowed. See net. createServer() and the 'end' event for details. Default: false .
The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).
I had the same issue. I ended up doing this as a fix:
const _connect = () => {
const socket = new net.Socket();
socket.connect(this.port, this.host);
socket.setTimeout(10000);
socket.on('timeout', () => {
socket.destroy();
_connect();
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With