4

I created a Websocket service class using KOUSH library https://github.com/koush/AndroidAsync#can-also-create-web-sockets

Now I want to check if the Websocket is connected to my server from my mainActivity. Do you know how I can check this? thanks

AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", new WebSocketConnectCallback() {
    @Override
    public void onCompleted(Exception ex, WebSocket webSocket) {
        if (ex != null) {
            ex.printStackTrace();
            return;
        }
        webSocket.send("a string");
        webSocket.send(new byte[10]);
        webSocket.setStringCallback(new StringCallback() {
            public void onStringAvailable(String s) {
                System.out.println("I got a string: " + s);
            }
        });
        webSocket.setDataCallback(new DataCallback() {
            public void onDataAvailable(ByteBufferList byteBufferList) {
                System.out.println("I got some bytes!");
                // note that this data has been read
                byteBufferList.recycle();
            }
        });
    }
});
NorthCat
  • 9,643
  • 16
  • 47
  • 50
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • Just try to use it and catch the exception. That tells you if it is it connected. Anything else is trying to predict the future. It might be up when you test and down when you go to use it. What use is that? – user207421 Jun 13 '17 at 23:40

2 Answers2

0

You can do something like this:

if (mWebSocketClient.isOpen) {
         mWebSocketClient.send(jObj.toString())
} else {
   try {
         client.connectWebSocket(p0)
         mWebSocketClient.send(jObj.toString())
   } catch (e : Exception){
        Toast.makeText(p0, "not connected $e", Toast.LENGTH_LONG).show()
   }
}
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
-1

The solution is to save the result of this Function to a local Websocket Variable , and then you check its status :

WebSocket webSocket=null;
try {
    webSocket=AsyncHttpClient.getDefaultInstance().websocket(get, "my-protocol", new WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                ex.printStackTrace();
                return;
            }
            webSocket.send("a string");
            webSocket.send(new byte[10]);
            webSocket.setStringCallback(new StringCallback() {
                public void onStringAvailable(String s) {
                    System.out.println("I got a string: " + s);
                }
            });
            webSocket.setDataCallback(new DataCallback() {
                public void onDataAvailable(ByteBufferList byteBufferList) {
                    System.out.println("I got some bytes!");
                    // note that this data has been read
                    byteBufferList.recycle();
                }
            });
        }
    }).get();
}catch(InterruptedException e){
//handle exception 
}catch(ExecutionException e){
//handle exception
}

if(websocket!=null && websocket.isOpen()){
//websocket is working fine 
}

you can use other statements like websocket.isClosed();

Said Bouigherdaine
  • 637
  • 1
  • 8
  • 22
  • `isClosed()` doesn't tell you whether the peer has disconnected. This code writes a string and 10 bytes of junk. The peer has to be prepared to receive that. – user207421 Jun 13 '17 at 23:39