5

I am trying to connect with secure websocket connection wss:// in android using org.java_websocket.client.WebSocketClient API, but unable to connect with https. However it is working fine with ws://.. Here is my code.

private void connect(String websocketEndPointUrl) throws Exception {
    URI uri;
    try {

        websocketEndPointUrl="wss://echo.websocket.org:443";
        Log.i(TAG, " WSURL: " + websocketEndPointUrl);

        uri = new URI(websocketEndPointUrl);
    } catch (URISyntaxException e) {
        Log.e(TAG, e.getMessage());
        return;
    }

    mWebSocketClient = new WebSocketClient(uri,new Draft_17()) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
         }

        @Override
        public void onMessage(String s) {
            //final String message =s;

         }

        @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 am using online test websocket url: ws://echo.websocket.org (port 80) // working with that wss://echo.websocket.org (port 443) As per my observation there is no need of certificate required in my code. Can anyone suggest me what is a reason and how i can fix this.

2 Answers2

9

Find a solution. I don't know why this is not a part of the documentation. You just need to set setWebSocketFactory after WebSocketClient initialization and before the .connect() method

mWebSocketClient = new WebSocketClient(uri,new Draft_17()) 
{
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        Log.i("Websocket", "Opened");
    }

    @Override
    public void onMessage(String s) {
        //final String message =s;

    }

    @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());
    }
};

if (websocketEndPointUrl.indexOf("wss") == 0) 
{
    try {
        SSLContext sslContext = SSLContext.getDefault();
        mWebSocketClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
} 

mWebSocketClient.connect();
darrenp
  • 4,265
  • 2
  • 26
  • 22
  • 1
    This is crazy. I've searched the whole internet to find this trick. Thanks anyway. – Azadeh Radkianpour Nov 29 '17 at 15:31
  • @Azade what is the package for WebSocketClient ? – WISHY Oct 22 '20 at 12:27
  • 1
    @WISHY import org.java_websocket.client.WebSocketClient; Gradle Dependency: implementation "org.java-websocket:Java-WebSocket:1.3.0" Other packages required for the above code(Just for the information): import org.java_websocket.client.DefaultSSLWebSocketClientFactory; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; import java.net.URISyntaxException; import java.security.NoSuchAlgorithmException; import javax.net.ssl.SSLContext; – abhy Nov 01 '20 at 16:10
0

In my case, I need two thing to fix "Trust anchor for certification path not found" error when websocket connect():

  1. HttpsURLConnection requests that particular wss host (but in https:// form) successfully at least once.
  2. Then dosetWebSocketFactory() as mentioned in accepted answer. Which this extra method (plus new Draft_17()) only appeared in library version org.java-websocket:Java-WebSocket:1.3.0, not 1.4.0.

Note that don't test with allowAllSSL() like this answer do, which affect the two thing above not working.

林果皞
  • 7,539
  • 3
  • 55
  • 70