Retry socket.io connection when loading client page and shutting down server

I am using SocketIO in a LAN application.

If the client starts and the server is turned off, how can I get it to try to connect again?

The connect_failed event does not fire because I get a GET error in socket.io: " http://172.20.2.10:8080/socket.io/1/?t=1348654723279 "

So I tried:

var socket = io.connect('172.20.2.10',{port:8080}); var connected = false; socket.on('connect',function(){ connected = true; }); socket.on('error', function (err) { reconnect(); }); function reconnect(){ if (!connected){ var t = setTimeout( function() { socket = io.connect('172.20.2.10',{port:8080}); reconnect(); console.log("Socket REC"); },1000); } } 

But it does not reconnect, I think this is due to a JS error ...

I try to avoid refreshing the page if possible.

+6
source share
1 answer

we can manually use the reconnect method of the socket instance.

 socket.on('error', function (err) { socket.socket.reconnect(); }); 
+18
source

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


All Articles