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.
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).
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With