Java 如何在android上正确使用Autobahn实现PubSub?

Java 如何在android上正确使用Autobahn实现PubSub?,java,android,publish-subscribe,autobahn,wamp-protocol,Java,Android,Publish Subscribe,Autobahn,Wamp Protocol,我一直在努力寻找一种很好的方法,通过Autobahn for android实现PubSub。我目前正在使用单例模式在我的整个应用程序中使用相同的高速公路连接。我接到了电话,订阅工作正常,但当我取消订阅,然后回到同一个片段,并尝试再次订阅时,它就不起作用了。下面是我目前的高速公路等级: package nl.w3s.hulpverlener.utils; import nl.w3s.hulpverlener.helper.DebugHelper; import android.util.Log

我一直在努力寻找一种很好的方法,通过Autobahn for android实现PubSub。我目前正在使用单例模式在我的整个应用程序中使用相同的高速公路连接。我接到了电话,订阅工作正常,但当我取消订阅,然后回到同一个片段,并尝试再次订阅时,它就不起作用了。下面是我目前的高速公路等级:

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);
    }
}
包nl.w3s.hulpverlener.utils;
导入nl.w3s.hulpverlener.helper.DebugHelper;
导入android.util.Log;
进口德.塔文多.高速公路.高速公路;
导入de.tavendo.autobahn.autobahn.SessionHandler;
导入de.tavendo.autobahn.AutobahnConnection;
进口tavendo.autobahn.Autobahnopions;
公共末级高速公路服务{
专用静态高速公路服务实例;
专用静态高速公路连接;
私人高速公路选项;
私有布尔连接=假;
专用字符串url=”http://johelpen.w3s.nl/";
私有字符串websocketUrl;
私人高速公路服务(){
连接=新的高速公路连接();
选项=新建高速公路选项();
options.setReceiveTextMessagesRaw(true);
websocketUrl=CommonUtilities.STAGING_WEBSOCKET_URL;
connect();
}
公共静态高速公路服务getInstance(){
if(实例==null)
实例=新的AutobahnService();
其他的
connect();
返回实例;
}
公共void connect(){
如果(!connection.isConnected()){
connection.connect(websocketUrl,新的SessionHandler(){
@凌驾
公共开放(){
连接=真;
Log.i(DebugHelper.TAG_DEBUG,“CONNECTED”);
}
@凌驾
公共void onClose(int p_intCode,字符串p_strReason){
连接=错误;
Log.i(DebugHelper.TAG_DEBUG,“断开”);
}
},选项);
}
}
public void doCall(最终字符串callType、最终类classRef、最终Autobahn.CallHandler autobahnEventHandler、最终对象…参数){
call(url+“#”+callType,classRef,autobahnEventHandler,参数);
}
public void doSubscribe(最终字符串callType、最终类classRef、最终Autobahn.EventHandler autobahnEventHandler){
订阅(url+callType,classRef,autobahnEventHandler);
}
public void doUnsubscribe(最终字符串callType){
连接.取消订阅(url+callType);
}
}
当我查看日志时,它在取消订阅和重新订阅时不会断开连接