0

I wrote the next code:

socket.setTimeout(2000);
socket.on('timeout', function() {   
    console.log("Timeout");
});
    socket.on('data', function(data){})

I send a data to my socket, but when I finish to send this data, the timeout doesn't occuer.

There is no socket.end() in my code.

1 Answer 1

1

The easiest way is to pass the timeout callback as a method straight to setTimeout where it will be added as a one-time listener to 'timeout';

socket.setTimeout(2000, function() {  
    console.log("Timeout");
});
socket.on('data', function(data){})

I've never seen the syntax of listening to the '' event before (I probably missed something), but the event for a timeout if you want to listen with it using on should be called 'timeout'.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I correct the last issue. I try your idea, but its still the same.
@user2450886 Sounds like there may be something not showing in your displayed code that's preventing the event from triggering. Sadly hard to debug then :-/
There is other command which close the socket besides socket.end() and socket.destroy()? As I understand, socket.write doesn't close the socket.
@user2450886 No, it shouldn't close unless there's an error transmitting the data, in which the error handler should be invoked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.