websocket支持wss需要在
WebSocketClient.connect();
之前配置
SSLContext sslContext = null; try { sslContext = SSLContext.getInstance("TLS"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } try { sslContext.init(null, new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }, new SecureRandom()); } catch (KeyManagementException e) { e.printStackTrace(); } SSLSocketFactory factory = sslContext.getSocketFactory(); try { webSocketClient.setSocket(factory.createSocket()); } catch (IOException e) { e.printStackTrace(); }
这样websocket就可以连接wss。