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 !
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0