我正在尝试将我的Android客户端与Meteor Server连接。
代码成功处理marshmallow以上的设备。 但不适用于棉花糖和以下设备。
我正在使用this库连接我的meteor服务器
当我在nougat以下的设备上尝试下面的实现时,我得到以下错误
javax.net.ssl.SSLProtocolException:SSL握手已中止: ssl = 0xb4067000:SSL库失败,通常是协议错误 错误:14077102:SSL例程:SSL23_GET_SERVER_HELLO:不受支持 protocol(external / openssl / ssl / s23_clnt.c:740 0xabeeddd4:0x00000000)
String exampleUri = "wss://myurl";
WebSocketFactory webSocketFactory = new WebSocketFactory();
webSocketFactory.setSocketFactory(new TLSSocketFactory());
webSocketFactory.setVerifyHostname(false);
WebSocket ws = webSocketFactory
.setConnectionTimeout(25000)
.createSocket(exampleUri)
.setPingInterval(20 * 1000)
.addListener(new WebSocketAdapter() {
// A text message arrived from the server.
public void onTextMessage(WebSocket websocket, String message) {
System.out.println(message);
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
super.onError(websocket, cause);
}
@Override
public void onConnectError(WebSocket websocket, WebSocketException exception) throws Exception {
super.onConnectError(websocket, exception);
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
super.onConnected(websocket, headers);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_LONG).show();
Log.e("Connected", "true");
}
});
}
});
ws.clearProtocols();
ws.connectAsynchronously();
return true;
} catch (Exception e1) {
e1.printStackTrace();
}
TLSSocketFacory.java
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket());
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if (socket != null && (socket instanceof SSLSocket)) {
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1","TLSv1.2"});
}
return socket;
}
}
任何帮助都会得到很高的评价