mkuklis / phonegap-websocket

Websocket PhoneGap plugin for Android
203 stars 78 forks source link

Keeping socket while app pauses and resumes #33

Open s1na opened 10 years ago

s1na commented 10 years ago

I'm using socket.io, when the user closes the application(pause) and then resumes, the old socket is replaced with a new one, and another socket is created also.

Is there some way I could keep the socket in the background? Or even destroy it before pause?

mkuklis commented 10 years ago

You could use http://docs.phonegap.com/en/3.2.0/cordova_events_events.md.html#pause to detect when the app is pausing and disconnect then.

s1na commented 10 years ago

Tried that but failed. The actual websockets are created in the java plugin, right? That's why when app goes to pause they're still alive. If I were to change the plugin and provide an api for getting the active socket, that'd do it?

mkuklis commented 10 years ago

By default when your app goes to pause the socket will stay open (it should not be closed if it is then there is something wrong going on with the plugin). Have you tried to bind to pause event and close your socket from socket.io:

document.addEventListener("pause", function () {
  socket.disconnect(); // disconnect comes from socket.io api
}, false);

This should propagate to Java side and close the socket connection.