2

I have been struggling to find a good way of implementing PubSub with Autobahn for android. I am currenty using the Singleton pattern to use the same AutobahnConnection in my whole app. I got the calls and subscribing working but when i unsubscribe and then come back to the same fragment and try to subscribe again it doesnt work. Below my current Autobahn Class:

package nl.w3s.hulpverlener.utils;

import nl.w3s.hulpverlener.helper.DebugHelper;
import android.util.Log;
import de.tavendo.autobahn.Autobahn;
import de.tavendo.autobahn.Autobahn.SessionHandler;
import de.tavendo.autobahn.AutobahnConnection;
import de.tavendo.autobahn.AutobahnOptions;

public final class AutobahnService{
    private static AutobahnService INSTANCE;
    private static AutobahnConnection connection;

    private AutobahnOptions options;
    private boolean connected = false;

    private String url = "http://johelpen.w3s.nl/";
    private String websocketUrl;

    private AutobahnService() {
        connection  = new AutobahnConnection();
        options = new AutobahnOptions();
        options.setReceiveTextMessagesRaw(true);

        websocketUrl = CommonUtilities.STAGING_WEBSOCKET_URL;
        connect();
    }

    public static AutobahnService getInstance() {
        if(INSTANCE == null)
            INSTANCE = new AutobahnService();
        else
            INSTANCE.connect();

        return INSTANCE;
    }

    public void connect() {
        if(!connection.isConnected()) {
            connection.connect(websocketUrl, new SessionHandler() {
                @Override
                public void onOpen() {
                    connected = true;
                    Log.i(DebugHelper.TAG_DEBUG, "CONNECTED");
                }

                @Override
                public void onClose(int p_intCode, String p_strReason) {
                    connected = false;
                    Log.i(DebugHelper.TAG_DEBUG, "DISCONNECTED");
                }
            }, options);
        }
    }

    public void doCall(final String callType, final Class<?> classRef, final Autobahn.CallHandler autobahnEventHandler, final Object... arguments) {
        connection.call(url + "#" + callType, classRef, autobahnEventHandler, arguments);
    }

    public void doSubscribe(final String callType, final Class<?> classRef, final Autobahn.EventHandler autobahnEventHandler) {
        connection.subscribe(url + callType, classRef, autobahnEventHandler);
    }

    public void doUnsubscribe(final String callType) {
        connection.unsubscribe(url + callType);
    }
}

When I look at my logs it doesnt disconnect while unsubscribing and resubscribing.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
Fergers
  • 473
  • 2
  • 7
  • 25

0 Answers0