Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Socket Connection failed from Android Client

I am developing an Android client for a server where the requirement is continuous exchange of audio stream to the WebSockets-based server.

While connection with web sockets the android client throws the following error.

Closed draft org.java_websocket.drafts.Draft_10@b2fe9b40 refuses handshake

But I tried with following web socket uri. Connection getting succeeded. ws://echo.websocket.org

Code:

URI uri;
    try {
        // uri = new URI(
        // "ws://echo.websocket.org");
        uri = new URI(
                "ws://serverIP:9090/WebRtc/serverendpoint");

    } catch (URISyntaxException e) {
        e.printStackTrace();
        return;
    }

    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " "
                    + Build.MODEL);
        }

        @Override
        public void onMessage(String s) {
            final String message = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView textView = (TextView) findViewById(R.id.messages);
                    textView.setText(textView.getText() + "\n" + message);
                }
            });
        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.i("Websocket", "Closed " + s);
        }

        @Override
        public void onError(Exception e) {
            Log.i("Websocket", "Error " + e.getMessage());
        }
    };
    mWebSocketClient.connect();

I tried the echo test from browser for the web socket(ws://serverIP:9090/WebRtc/serverendpoint) I have used. It's getting connected properly. But When I try that from both mobile or emulator, nothing works.

Please help me on this.

like image 639
M Vignesh Avatar asked Sep 12 '14 06:09

M Vignesh


People also ask

What causes a WebSocket to fail?

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).


2 Answers

I just found another simple way to get rid of this issue.

Change following line

mWebSocketClient = new WebSocketClient(uri){

to

mWebSocketClient = new WebSocketClient(uri, new Draft_17()){

I found the hint in my Serverlogs which said:

User Agent: [unset] requested WebSocket version [8], Jetty supports version: [13]

As you can see in the link below, Version 8 is Draft_10, Version 13 is Draft_17.

Link to supported Websocket-Versions

like image 128
AlbAtNf Avatar answered Sep 24 '22 16:09

AlbAtNf


I found the issue with this.

Actually the problem with the Java provided web socket jar file.

Instead of that I have used the Autobahn (Android Specific Web Socket Open Source https://github.com/tavendo/AutobahnAndroid).

Now am able to connect with the web socket server.

like image 45
M Vignesh Avatar answered Sep 22 '22 16:09

M Vignesh