Unable to connect websocket with wss in android

风格不统一 提交于 2019-12-06 03:42:13

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!