Background
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.
Problem
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!
Code
let socket;
const start = async function ( config ) {
await connecToBarrier( config.barrier )
.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 ) => {
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();
Question
How do I detect if a connection died?
via Flame_Phoenix
No comments:
Post a Comment