Disconnection issue
After disconnecting socket, it keeps listening and runs call(Object... args).
For connection:
Global.socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
if(Global.socket != null && Global.socket.connected()){
Global.socket.emit("deviceTokens", jsonObject);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
Log.i("response", "connected at " + currentDateTimeString);
}
}
}).on("refresh", new Emitter.Listener() {
@Override
public void call(Object... args) {
if(Global.socket != null && Global.socket.connected()){
Global.socket.emit("deviceTokens", jsonObject);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
Log.i("response", "refreshed at " + currentDateTimeString);
Intent intent = new Intent(context.getApplicationContext(), FirstRunActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("refresh", "server");
Util.deleteExistingFile(context);
ShopManager.getInstance().unregisterAds(context);
Activity act = (Activity) context;
act.startActivity(intent);
act.finish();
}
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
Log.i("response", "disconnected at " + currentDateTimeString);
Global.socket = null;
}
});
Global.socket.connect();
For disconnection:
if (Global.socket != null) {
Global.socket.disconnect();
}
Btw, I am using io.socket:socket.io-client:1.0.0.
Hi...
Calling disconnect alone won't work...
You need to call the off method on the Emitter Listeners
socketInternal.connect();
socketInternal.on(Socket.EVENT_CONNECT, onConnected);
socketInternal.on(ON_NEW_USER_TO_CLIENT, onNewUserToClient);
socketInternal.on(MESSAGE_TO_CLIENT, onGetMessageToClient);
socketInternal.disconnect();
socketInternal.off(Socket.EVENT_CONNECT, onConnected);
socketInternal.off(ON_NEW_USER_TO_CLIENT, onNewUserToClient);
socketInternal.off(MESSAGE_TO_CLIENT, onGetMessageToClient);
And I feel it's better if you make your socket into library then initialize it from the Application instance
Nope, unfortunately it has not worked in my app. I've called .off methods after disconnect() but it still running. I mean after the onDisconnect method my app still doing smt backgroud. I can close it manually.
Now i'm using version 0.6.0. There is no problem like this in this version.