Android autobahn websocket doesn't disconnect

1.1k views Asked by At

I'm using websocket in my app to check new messages. Websocket implemented in my Service class, when i want to stop service (using stopService in Activity) it should call disconnect websocket, but method onClose doesn't called

Here is some code:

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    start();

    super.onStartCommand(intent, flags, startId);

    return Service.START_STICKY;
}

@Override
public void onDestroy() {
    Log.i("tag", "onDestroy service start");

    Intent intent = new Intent(this, RepeatingAlarmService.class);

    if (alarmManager != null) {
        alarmManager.cancel(PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0));
    }

    mConnection.disconnect();  // this line doesn't work ???

    super.onDestroy();

    Log.i("tag", "service stopped");
}


private void start() {
    final String uri = "ws://ws.blabla.com"
    try {

        mConnection.connect(uri, new WebSocketConnectionHandler(){
            @Override
            public void onOpen() {
                Log.i("tag", "Socket open"); // this line work well

                super.onOpen();
            }

            @Override
            public void onClose(int code, String reason) {
                Log.i("tag", "Socket closed " + String.format("code: %d, reason: %s", code, reason));  // this line doesn't been printed
                }

                super.onClose(code, reason);
            }

            @Override
            public void onTextMessage(String payload) {              
                Log.i("tag", payload); // this line work well

                super.onTextMessage(payload);
            }
        });
    } catch(Exception ex) {

    }
}
1

There are 1 answers

6
Yuriy Kolbasinskiy On BEST ANSWER

The answer is: use only latest version of external libraries.

In gradle build file i'm using maven repository "jitpack.io"

repositories {
        maven {
            url "https://jitpack.io"
        }
    }

I write this line compile 'com.github.tavendo:AutobahnAndroid:v0.5.2' at dependencies section to tell gradle that he should download dependendy from jitpack repository.

The latest version on Github is 0.5.2 but if you download it from github (not from jitpack.io) you will see the difference.