-
-
Notifications
You must be signed in to change notification settings - Fork 657
Description
If there is a possibility that this.stream.setKeepAlive is executed before the successful creation of the connection, and in that case, it becomes ineffective, you can modify your code to ensure that this.stream.setKeepAlive is called only after the connection is established.
You can move the this.stream.setKeepAlive statement inside the callback function of net.connect, ensuring that it is executed after the connection is successfully established. This way, you can ensure that the setting takes effect at the right time.
Here's an example of how you can modify your code:
const net = require('net');
const socket = net.connect(port, host, () => {
// Connection is successfully established
socket.setKeepAlive(true);
// Rest of your code
});
socket.on('error', (err) => {
// Handle connection errors
});
socket.on('close', () => {
// Handle connection closure
});