Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - Node.js - Socket.IO 1.0 - Timeout event

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...