Socketio client: how to handle socketio server down

I have a socket server / client working well together, however I want to start recording events when the server is offline when the page loads or during a normal run.

I include the remote socket.io code in my header:

<script src="<?=NODE_HOST?>/socket.io/socket.io.js"></script> <script> var nodeHost = '<?=NODE_HOST?>'; </script> 

And in my client controller I have

  if (typeof io! = 'undefined')
         this.socket = io.connect (this.settings.server);
     else
         this.handleDisconnect ();

A function that I should try to reconnect again and again if: a) the connector is disconnected during normal operation or b) the server is disconnected when the page loads

  botController.prototype.handleDisconnect = function () {

     $ .getScript (nodeHost + "/ socket.io/socket.io.js").done(function(script, textStatus) {

         bot.control.socket = io.connect (bot.control.settings.server);

     }). fail (function (jqxhr, settings, exception) {
         setTimeout (function () {
             bot.control.handleDisconnect ();
         }, 5000);
     });
 }

Am I doing it right?

The main problem that I am currently facing (which made me create this question) is my code errors when loading the page when the server is offline, because I have features such as:

  socket.on (... 

If the socket does not exist yet. Can I wrap them in a function and call it when I discover that a global socket object exists on successful reconnection? Does it matter if this function, which contains socket.on ..., is called several times (if the server does not work more than once during operation)?

+4
source share
1 answer

OK I was able to find this solution that seems to work well using yepnope, which I already had using Modernizr (it also handles the cross-domain problem).

 <script src="<?=NODE_HOST?>/socket.io/socket.io.js"></script> <script> var nodeHost = '<?=NODE_HOST?>'; </script> 
  // Attempt to connect to nodejs server
 botController.prototype.start = function () {    

     // Is our nodejs server up yet?
     if (typeof io! = 'undefined') {
         this.socket = io.connect (this.settings.server);
         this.startSocketEvents ();
     } else {
         this.handleDisconnect ();
     }
 }

 // Our connection to the server has been lost, we need to keep 
 // trying to get it back until we have it!
 botController.prototype.handleDisconnect = function (destroySocketObject) {

     if (destroySocketObject === undefined)
         destroySocketObject = true;

     // Destroy any cached io object before requesting the script again
     if (destroySocketObject)
         io = undefined;

     yepnope.injectJs (nodeHost + "/ socket.io/socket.io.js",
         function (result) {

             // Did it actually download the script OK?
             if (typeof io! = 'undefined') {
                 bot.control.socket = io.connect (bot.control.settings.server);
                 bot.control.startSocketEvents ();
             } else {
                 setTimeout (function () {
                     bot.control.handleDisconnect (false);
                 }, 5000);
             }
         }
     );

Where the startSocketEvents () function contains all my socket.on events

0
source

Source: https://habr.com/ru/post/1481564/


All Articles