Handle reboot socket.io browser

Is there a way in socket.io to create a timeout in a disconnected event, and then check if the user has been reconnected?

The idea is to emit data / save user state in the database only if the user does not connect after a timeout

Edit : Followed by @Are Wojciechowski's answer, I ended up with several tabs and an F5 fluorescence processor

https://gist.github.com/foohey/7696811

+18
Nov 28 '13 at 7:30
source share
1 answer

There is socket.on('disconnect', function () { ... }); . So what you can just do

 socket.on('disconnect', function () { setTimeout(function () { //do something }, 10000); }); 



EDIT 1:

I get it now. So maybe you should do something like this:

Client:

 //right after connection socket.emit('register', localstorage.getItem('gameUniqueId')); //somewhere, when game starts var randomlyGeneratedUID = Math.random().toString(36).substring(3,16) + +new Date; localStorage.setItem('gameUniqueId', randomlyGeneratedUID); 

Server:

 io.sockets.on('connection', function (socket) { var player = null; socket.on('register', function (data) { if (data !== null) { //there was something in localstorage if (game.Players.existsUID(data)) { player = game.Players.getByUID(data); player.disconnected = false; } else { //timed out, create new player } } else { //localStorage is not set, create new player } }); socket.on('disconnect', function () { player.disconnected = true; setTimeout(function () { if (player.disconnected) player.delete(); }, 10000); }); }); 
+26
Nov 28. '13 at 8:34
source share



All Articles