无法在Android中将WebSocket与WSS连接 [英] Unable to connect websocket with wss in android

查看:1301
本文介绍了无法在Android中将WebSocket与WSS连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用org.java_websocket.client.WebSocketClient API与android中的安全websocket连接wss://进行连接,但无法与https连接。

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

我正在使用在线测试websocket网址:
ws:// echo.websocket.org(端口80)//与
一起使用wss://echo.websocket.org(端口443)
根据我的观察,我的代码中不需要证书。谁能建议我这是什么原因以及如何解决这个问题。

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.

推荐答案

查找解决方案。我不知道为什么这不是文档的一部分。您只需要在WebSocketClient初始化之后和 .connect()方法之前设置 setWebSocketFactory

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

这篇关于无法在Android中将WebSocket与WSS连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆