Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io connect with server offline

How do I detect if the server is offline, or for some other reason cannot connect. My code looks something like this.

this.socket = io.connect(connectionInfo, {
    reconnect:false
});

It does not throw any error, so a try/catch clause is not working.

like image 643
Adam Avatar asked Aug 15 '13 07:08

Adam


People also ask

Does Socket.IO need Internet?

socket.io only works when connected to the internet #2309.

Why is Socket.IO not connecting?

Problem: the socket is not able to connect​ Possible explanations: You are trying to reach a plain WebSocket server. The server is not reachable. The client is not compatible with the version of the server.


1 Answers

Use

  • this.socket.on("connect", callback) to catch connection events
  • this.socket.on("disconnect", callback) to catch disconnection events
  • this.socket.on("connect_failed", callback) to catch failed connection attempts
  • this.socket.io.on("connect_error", callback) to catch if the server is offline

You can find all events, at https://github.com/LearnBoost/socket.io/wiki/Exposed-events

like image 65
Kurt Pattyn Avatar answered Sep 21 '22 10:09

Kurt Pattyn