简体   繁体   English

无法在android中将websocket与wss连接

[英]Unable to connect websocket with wss in android

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.我正在尝试使用 org.java_websocket.client.WebSocketClient API 在 android 中连接安全的 websocket 连接 wss://,但无法连接 https。 However it is working fine with ws://.. Here is my code.但是它与 ws:// 一起工作正常。这是我的代码。

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.我正在使用在线测试 websocket url: ws://echo.websocket.org (port 80) // 使用 wss://echo.websocket.org (port 443) 根据我的观察,不需要证书在我的代码中。 Can anyone suggest me what is a reason and how i can fix this.任何人都可以建议我是什么原因以及如何解决这个问题。

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您只需要在 WebSocketClient 初始化之后和.connect .connect()方法之前设置setWebSocketFactory

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

In my case, I need two thing to fix "Trust anchor for certification path not found " error when websocket connect() :就我而言,当 websocket connect()时,我需要做两件事来修复“未找到认证路径的信任锚”错误:

  1. HttpsURLConnection requests that particular wss host (but in https:// form) successfully at least once. HttpsURLConnection至少成功请求该特定 wss 主机(但以https://形式)一次。
  2. Then do setWebSocketFactory() as mentioned in accepted answer.然后按照setWebSocketFactory()接受的答案中所述执行setWebSocketFactory() 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 .这个额外的方法(加上new Draft_17() )只出现在库版本org.java-websocket:Java-WebSocket:1.3.0 ,而不是1.4.0

Note that don't test with allowAllSSL() like this answer do, which affect the two thing above not working.请注意,不要像这个答案那样使用allowAllSSL()测试,这会影响上述两件事不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM