fantom
fantom

Reputation: 258

How reconnect from server to client

I'am able to detect when client disconnect from a server by this code:

self._session.socket.on("close", function() {
    console.log("client disconnected");    
}

But how can I try reconnect to the disconnected client?

Upvotes: 2

Views: 277

Answers (2)

Tarang
Tarang

Reputation: 75945

If the client got disconnected for some reason (internet connection disruption/server issues) it will automatically reconnect on its own. To see how many attempts have been made or the status have a look at http://docs.meteor.com/#meteor_status

Since version 0.6.3 if the internet was disconnected. As soon as the internet is back it will attempt to reconnect too.

To reconnect from your code somewhere you can run Meteor.reconnect() from the client.

Unfortunately the meteor client can't listen for connections from the server so the server can't initiate a reconnection, you need some kind of connection to a server to send a message to the client to do something such as a reconnection.

Upvotes: 1

Fernando Jorge Mota
Fernando Jorge Mota

Reputation: 1554

You cannot connect from server to client as client isn't listening to the websocket, but just connecting (to the server).

However, you can put a code in your client to reconnect it at onclose (or just close) event. Generally this occurs by recreating the WebSocket object in the client with the correct parameters.

Something as:

function connect(){
    var mywebsocket = new WebSocket("ws://(your url)");
    // ... my callbacks and functions...
    mywebsocket.onclose = connect; // or arguments.callee
}
connect();

Should work correctly. ;)

Good luck.

Upvotes: 1

Related Questions