1

我有个问题。如何使用自签名创建 SSL websocket 客户端。

mServer = new AsyncHttpServer();
 mServer.websocket("/get", new AsyncHttpServer.WebSocketRequestCallback() {
        @Override
        public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
            _sockets.add(webSocket);

            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    try {
                        if (ex != null)
                            Log.e("WebSocket", "Error");
                    } finally {
                        _sockets.remove(webSocket);
                    }
                }
            });

            webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(final String s) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                        }
                    });

                    if ("Hello Server".equals(s))
                        webSocket.send("Welcome Client!");
                    else
                        webSocket.send(s);
                }
            });


            btnTest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    webSocket.send("I am server!!!");
                }
            });
        }
    });

   // mServer.listen(8080);
    SSLContext sslContext = null;
    MySSLContext mySSLContext = new MySSLContext();

    try {
        sslContext = mySSLContext.getSSLContext();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    mServer.listenSecure(8080, sslContext);

我的服务器编码

public SSLContext getSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, new TrustManager[]{ new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }}, new SecureRandom());

    return sslContext;
}

获取 SSLContext 方法

AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
    AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
    MySSLContext mySSLContext = new MySSLContext();
    SSLContext sslContext = null;
    try {
        sslContext = mySSLContext.getSSLContext();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    sslSocketMiddleware.setSSLContext(sslContext);

    client.insertMiddleware(sslSocketMiddleware);


    client.websocket("https://192.168.88.11:8080/get", null, new AsyncHttpClient.WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                ex.printStackTrace();
                return;
            }


            webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(final String s) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                        }
                    });
                    Log.d("StringCallback", s);
                }
            });
            webSocket.send("Hello Server");

            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Log.d(TAG, "Closed/onCompleted:" + ex.getMessage());
                }
            });

            webSocket.setEndCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Log.d(TAG, "End/onCompleted:" + ex.getMessage());
                }
            });
        }

    });

这是我的客户编码

然后,我得到了

javax.net.ssl.SSLException:握手期间套接字关闭..

4

0 回答 0