连接stomp客户端后添加主题/订阅

时间:2017-01-31 08:41:41

标签: android websocket stomp java-websocket

我目前正在使用 https://github.com/NaikSoftware/StompProtocolAndroid 使用STOMP连接websocket。我有一个简单的实现

public class TestActivity extends AppCompatActivity {

private StompClient mStompCLient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);

    mStompCLient = Stomp.over(WebSocket.class, BASE_URL);

    mStompCLient.topic("/topic/online/" + mSharedPreferences.getPrivateKey()).subscribe(new Subscriber<StompMessage>() {
        @Override
        public void onCompleted() {
            Log.i(TAG, "/topic/online/ onCompleted: ");
        }

        @Override
        public void onError(Throwable e) {
            Log.i(TAG, "/topic/online/ onError: " + e.getMessage());
        }

        @Override
        public void onNext(StompMessage stompMessage) {
            Log.d(TAG, "/topic/online/ onNext: " + stompMessage.getPayload());
String content = "";
JSONObject jsonResponse = null;
            try {
                jsonResponse = new JSONObject(stompMessage.getPayload());
                content = jsonResponse.getString("uri");
            } catch (JSONException e) {
                e.printStackTrace();
            }
listenToUpdatesFromFinalUri(content);
        }
    });

    mStompCLient.lifecycle().subscribe(lifecycleEvent -> {
        Log.i(TAG, "onCreate: " + lifecycleEvent.getMessage());
        switch (lifecycleEvent.getType()) {

            case OPENED:
                Log.d(TAG, "Stomp connection opened");
                break;

            case ERROR:
                Log.e(TAG, "Error", lifecycleEvent.getException());

                break;

            case CLOSED:
                Log.d(TAG, "Stomp connection closed : " + lifecycleEvent.toString() + " :msg: " + lifecycleEvent.getMessage() + " :escep: " + lifecycleEvent.getException() + " :headers: " + lifecycleEvent.getHandshakeResponseHeaders() + " :type: " + lifecycleEvent.getType());
                break;
        }
    });

mStompCLient.connect();
}

private void listenToUpdatesFromFinalUri(String content) {
    mStompCLient.topic(content).subscribe(new Subscriber<StompMessage>() {
                @Override
                public void onCompleted() {
                    Log.i(TAG," onCompleted: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.i(TAG, " onError: " + e.getMessage());
                }

                @Override
                public void onNext(StompMessage stompMessage) {
                    Log.d(TAG, " onNext: " + stompMessage.getPayload());
                }
            });
}

@Override
protected void onStop() {
    super.onStop();
    disconnectStomp();
}

private void disconnectStomp() {
    mStompCLient.disconnect();
}
}

这里我试图在建立连接后收听服务器发送的新订阅频道。如果在调用connect之前调用subscribe(),它就有效。但是在listenToUpdatesFromFinalUri()函数中订阅的最终uri /订阅频道不是静态的,所以我需要在连接之前不能添加订阅。我目前无法获得最终uri /订阅的回复。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

该问题已在新版本1.1.6中解决