我使用以下一组代码在我的应用程序中集成套接字,套接字在连接后断开连接,并将传输错误作为唯一可见的日志。但是,对于相同的URL,iOS应用程序正在顺利运行。 我在iOS应用中使用的代码如下:
[.reconnects(true), .reconnectAttempts(5), .reconnectWait(3), .log(false), .forcePolling(false), .forceWebsockets(false),.forceNew(false),.secure(false),.selfSigned(false ,.path("/socket.io-client/")]
public void socketConnection() {
try {
HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
SSLContext mySSLContext = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
System.out.println("===socket==checkClientTrusted=authType==" + authType);
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
System.out.println("===socket==checkServerTrusted=authType==" + authType);
}
}};
mySSLContext.init(null, trustAllCerts, new java.security.SecureRandom());
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.hostnameVerifier(myHostnameVerifier)
.sslSocketFactory(mySSLContext.getSocketFactory(), new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
try {
System.out.println("checkClientTrusted " + authType);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
try {
System.out.println("checkClientTrusted " + authType);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
})
.build();
IO.setDefaultOkHttpCallFactory(okHttpClient);
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.Options opts = new IO.Options();
opts.path = "/socket.io-client";
opts.secure = false;
opts.transports = new String[]{Polling.NAME};
opts.forceNew = true;
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
mSocket = IO.socket(Urls.socket_url, opts);
mSocket.on(Socket.EVENT_CONNECTING, onEvenConnecting);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onError);
mSocket.on(Socket.EVENT_CONNECT, onEvenConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.connect();
} catch (Exception e) {
e.printStackTrace();
}
}