Any way to check if TCP socket in NodeJS has been connected. As opposed to still connecting in transient. Am looking for a property as opposed to an emitted event. Is it even possible?
1 Answer
The manual shows you that an event is emitted for when the connection is established and that is how you should handle it the Node.js way (using non-blocking, asynchronous i/o)
There is though an undocumented way to check it, the socket object has a _connecting boolean property that if set to false means it has been connected already or failed.
Example :
var net = require('net');
//Connect to localhost:80
var socket = net.createConnection(80);
if(socket._connecting === true) {
console.log("The TCP connection is not established yet");
}
1 Comment
Alexander Mills
how to test if it is connected?