Android Socket.io Websocket Transport does not works in SSL

前端 未结 3 1836
予麋鹿
予麋鹿 2020-12-19 21:00

I\'m using gottox socket.io java client for an Android chat application.
I could connect to both web-socket and Xhr transport in HTTP mode. But when i switch to HTTPS o

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 21:37

    It works but you have to do some modifications on io.socket library. Instead of using the socketio.jar, import into src folder the io.socket library (You'll find inside socket.io-java-client package). There, you have to edit the WebsocketTransport class.

    Here you have the solution

    https://github.com/Gottox/socket.io-java-client/issues/60

     public WebsocketTransport(URI uri, IOConnection connection) {
        super(uri);
        this.connection = connection;
        SSLContext context = null;
        try {
            context = SSLContext.getInstance("TLS", "HarmonyJSSE");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } 
        try {
            context.init(null, null, null);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        if("wss".equals(uri.getScheme()) && context != null) {
            this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
        }
    }
    

    And remember to call the setDefaultSSLSocketFactory like this:

    socket = new SocketIO();
    socket.setDefaultSSLSocketFactory(SSLContext.getDefault());
    socket.connect("https://www.myHttpsServer.com:443/");
    

    Hope it helps someone ;)

提交回复
热议问题