有时我会收到此错误:
io.socket.engineio.client.EngineIOException:xhr轮询错误
我连接到套接字的代码:
try {
HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
SSLContext mySSLContext = null;
try {
mySSLContext = SSLContext.getInstance("TLS");
try {
mySSLContext.init(null, trustAllCerts, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder().hostnameVerifier(myHostnameVerifier).sslSocketFactory(mySSLContext.getSocketFactory()).build();
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.setDefaultOkHttpCallFactory(okHttpClient);
IO.Options opts = new IO.Options();
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
G.socket = IO.socket(G.SOCKET_ADDRESS, opts);
G.socket.connect();
} catch (URISyntaxException e) {
Log.i("errorCheck", "E: "+e);
e.printStackTrace();
}
但是有时候效果很好。在其他时候则不是。
我使用
HTTPS
,但是在443端口上不起作用。我从用户那里获得了所有权限,有时
socket.io
可以正常工作。我如何修复它以使其始终工作?
最佳答案
最后,我将传输方式从polling
更改为websocket
并解决了我的问题(客户端和服务器端)
安卓:
IO.Options opts = new IO.Options();
opts.transports = new String[] { WebSocket.NAME };
socket = IO.socket(SOCKET_ADDRESS, opts);
socket.connect();
服务器:
io.set('transports', ['websocket']);
关于android - Android:socket.io io.socket.engineio.client.EngineIOException:XHR轮询错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57637856/