Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using Socket.IO (the latest version 1.1.0) to exchange messages with an Android app (the client). I would like to set a timeout (5s for example) to check if my client is still connected or not (I would like to handle the case when the Android app crashes). Moreover, I would like to generate an event when a this timeout occurs. What I want to do looks like this:

1/ Set the timeout

var socket = require('socket.io')({
   //options go here
  'timeout': 5000 //set the timeout to 5s
});

2/ Handle timeout event:

socket.on('timeout', function(){
   //my treatment
});

But I don't find any implementation to handle the timeout.

Thanks for your help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
423 views
Welcome To Ask or Share your Answers For Others

1 Answer

That's not what the timeout setting handles. The timeout is how long the server will wait for a re-connect before it tears down that connection. You probably don't want to set it that short; possibly even leave it at the default.

Socketio automatically sends heartbeats by default. You really should just need to assign a function to run when the 'disconnect' message is received on the server side.

io.on('connection', function(socket){
       console.log('a user connected');
       socket.on('disconnect', function(){
           console.log('user disconnected');
       });
});

Check out this post for more information about the heartbeat: Advantage/disadvantage of using socketio heartbeats


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...