socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

Disconnection issue

Open emrealparslan93 opened this issue 8 years ago • 3 comments

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.

emrealparslan93 avatar Oct 05 '17 13:10 emrealparslan93

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

jirevwe avatar Oct 13 '17 17:10 jirevwe

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.

berkaydedeoglu avatar Nov 29 '17 18:11 berkaydedeoglu

Now i'm using version 0.6.0. There is no problem like this in this version.

berkaydedeoglu avatar Nov 29 '17 19:11 berkaydedeoglu