无法在Koush AndroidAsync中获得断开连接回调事件?

时间:2017-10-05 08:15:56

标签: android websocket androidasync-koush

我已经在我的android应用程序中使用koushikdutta库实现了websocket客户端。我希望在我的实现中获得自动重新连接功能但不知何故当我关闭移动wifi并打开后面我无法重新连接因为setClosedCallback / setEndCallback无法正常工作。这是我的实施:

 public void disconnectWebsocket() {
        if (mConnection != null) {
            mConnection.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Log.i(TAG, "Destroying websocket");
                    MiscUtils.displayToast(activity, "Destroying websocket", Toast.LENGTH_SHORT);
                }
            });
            mConnection.close();
            mConnection = null;
            authenticationInfo=null;
        }
    if (handler != null) {
        handler.removeCallbacks(runnableCallback);
    }
    handler = null;
}

private final TrustManager trm = new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    public void checkClientTrusted(X509Certificate[] certs, String authType) {}
    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
};

private void connectWebsocket() {
    disconnectWebsocket();

    if (handler != null) {
        handler.removeCallbacks(runnableCallback);
    }

    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trm }, null);
        AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(sslContext);
        AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setTrustManagers(new TrustManager[] {trm});
        AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
    catch (Exception e) {
        Log.e(TAG, "Unable to set sslcontext", e);
    }

    String reqUrl = MiscUtils.buildUrl( baseUrl, "websocket/graphtools/1");
    //reqUrl = reqUrl.replace("http://", "ws://");
    //reqUrl = reqUrl.replace("https://", "wss://");
    AsyncHttpRequest request = MiscUtils.extendRequest(new AsyncHttpGet(reqUrl), authenticationInfo);
    AsyncHttpClient.getDefaultInstance().websocket(request, null, new AsyncHttpClient.WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                Log.e(TAG, "Unable to connect to websocket", ex);
                if (handler != null) {
                    MiscUtils.displayToast(activity, "Unable to connect to weboscket, trying to reconnect", Toast.LENGTH_SHORT);
                    handler.postDelayed(runnableCallback, 2000);
                }
                return;
            }

            Log.i(TAG, "Websocket onCompleted");
            mConnection = webSocket;

             webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(String s) {
                    Log.i(TAG, "Ws string available: " + s);
                    if (activity != null) {
                        ((WebSocketHelper) ((AuthenticationHelper) activity).getZoneFragment()).parseWebSocketEvent(s);
                    }
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Log.i(TAG, "Websocket disconnected");
                    updateWebsocketStatusLabel(false);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    if (handler != null) {
                        Log.i(TAG, "Websocket closed");
                        MiscUtils.displayToast(activity, "Websocket connection lost, trying to reconnect", Toast.LENGTH_SHORT);
                        handler.postDelayed(runnableCallback, 1000);
                    }
                }
            });
            updateWebsocketStatusLabel(true);
        }
    });
}

Runnable runnableCallback = new Runnable() {
    @Override
    public void run() {
        connectWebsocket();
    }
};

有人可以告诉我这是实现此回调的正确方法吗?

0 个答案:

没有答案