How to simulate connection failure in Socket.IO

I am developing an application in which clients connect to a nodejs server through Socket.io and subscribe to various events. These subscriptions are quite complicated, so you cannot use the Socket.IO channel function.

This means that the customer must track their subscriptions and may have to re-subscribe when it has been disconnected. Unfortunately, I'm not quite sure how Socket.IO handles reconnection and exactly how transparent it is with the client.

So here is the question: how can I simulate a connection failure and force Socket.IO to reconnect?

+5
source share
2 answers

Socket.io . , , API.

. stackoverflow Socket.IO

0

, :

:

// the next 3 functions will be fired automatically on a disconnect.
// the disconnect (the first function) is not required, but you know, 
// you can use it make some other good stuff.

socket.on("disconnect", function() {
  console.log("Disconnected");
});

socket.on("reconnect", function() {
  // do not rejoin from here, since the socket.id token and/or rooms are still
  // not available.
  console.log("Reconnecting");
});

socket.on("connect", function() {
  // thats the key line, now register to the room you want.
  // info about the required rooms (if its not as simple as my 
  // example) could easily be reached via a DB connection. It worth it.
  socket.emit("registerToRoom", $scope.user.phone);
});

:

io.on('connection', function(socket){
  socket.on("registerToRoom", function(userPhone) {   
    socket.join(userPhone);   
  });
});

. .

( ) , , - .

-1

All Articles