I am using node.js and socket.io for building a game .
I have the code as below . The game is simple . It starts doing some animation as soon as it connects to the server and it keeps animating.
I want to make sure that the game fails gracefully if the internet connection goes down or if the server gets disconnected .And the game gets back once the connection is back up .
The animate function fetches content from the server . Hence it will not work properly if there is no connection . I want the animate function to get executed only when there is connection.
So I introduced a variable called connected , which is supposed to hold the status of the connection . if conncted=false , I stop the animation .
But now the problem is socket.io by itself tries to reconnect a few times . How do I set connected to false , during the reconnection process ?
Game.js
var connected = false; $(document).ready(function(){ //connect to socket server socket = io.connect('/gameserver'); socket.on('connect', function() { connected = true; animate(); }); socket.on('disconnect', function() { connected = false; socket.connect(); }); }); function animate() { .... ...... ........ if (( connected ) && (..... )) animate(); }