我正在编写一个需要连接到node.js服务器上运行的Socket.IO实例的Android应用程序。
尝试连接到实例并使用iOS设备传输数据可以完美运行,但是当我尝试使用Android设备时,它会失败。
我正在使用Native java client,这是我在android端使用的代码:
mManager = new Manager(new URI("https://example.com"));
mSocket = mManager.socket("/users");
// socket events listeners
mSocket.on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Caught EVENT_CONNECTING");
for (Object obj : args) {
Log.v(TAG, "Errors :: " + obj);
}
}
}).on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "connected to the backend");
Log.v(TAG, String.format("JSON Obj to emit: %s", jsonObject.toString()));
mSocket.emit("hello_packet", jsonObject);
}
}).on(Socket.EVENT_RECONNECTING, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Caught EVENT_RECONNECTING");
for (Object obj : args) {
Log.v(TAG, "Errors :: " + obj);
}
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Socket disconnected");
}
}).on(Socket.EVENT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Caught EVENT_ERROR");
for (Object obj : args) {
Log.v(TAG, "Errors :: " + obj);
}
}
}).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Caught EVENT_CONNECT_ERROR");
for (Object obj : args) {
Log.v(TAG, "Errors :: " + obj);
}
}
});
Log.v(TAG, "Connecting socket");
mSocket.connect();
每当我尝试连接时(基本上只要行mSocket.connect();
)日志就会打印以下行:
05-07 22:41:36.684 15552-15552/com.my.app V/Main Activity: Connecting socket
05-07 22:41:36.699 15552-15858/com.my.app V/Main Activity: Caught EVENT_CONNECTING
05-07 22:41:36.926 15552-15866/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:36.926 15552-15866/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:38.189 15552-15884/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:38.189 15552-15884/com.my.app V/Main Activity: Errors :: 1
05-07 22:41:38.207 15552-15887/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:38.208 15552-15887/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:39.518 15552-15911/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:39.518 15552-15911/com.my.app V/Main Activity: Errors :: 2
05-07 22:41:39.531 15552-15915/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:39.531 15552-15915/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:41.846 15552-15949/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:41.846 15552-15949/com.my.app V/Main Activity: Errors :: 3
05-07 22:41:41.857 15552-15953/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:41.857 15552-15953/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:46.863 15552-16025/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:46.864 15552-16025/com.my.app V/Main Activity: Errors :: 4
05-07 22:41:46.879 15552-16029/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:46.879 15552-16029/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:51.883 15552-16126/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:51.883 15552-16126/com.my.app V/Main Activity: Errors :: 5
05-07 22:41:51.895 15552-16130/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:51.895 15552-16130/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:41:56.900 15552-16236/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:41:56.900 15552-16236/com.my.app V/Main Activity: Errors :: 6
05-07 22:41:56.921 15552-16240/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:41:56.921 15552-16240/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:01.927 15552-16357/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:01.927 15552-16357/com.my.app V/Main Activity: Errors :: 7
05-07 22:42:01.945 15552-16361/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:01.945 15552-16361/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:06.951 15552-16466/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:06.952 15552-16466/com.my.app V/Main Activity: Errors :: 8
05-07 22:42:06.969 15552-16470/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:06.970 15552-16470/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:11.975 15552-16545/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:11.976 15552-16545/com.my.app V/Main Activity: Errors :: 9
05-07 22:42:11.994 15552-16549/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:11.994 15552-16549/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:17.000 15552-16629/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:17.000 15552-16629/com.my.app V/Main Activity: Errors :: 10
05-07 22:42:17.012 15552-16633/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:17.012 15552-16633/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:22.017 15552-16710/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:22.017 15552-16710/com.my.app V/Main Activity: Errors :: 11
05-07 22:42:22.033 15552-16714/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:22.033 15552-16714/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:27.039 15552-16788/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:27.039 15552-16788/com.my.app V/Main Activity: Errors :: 12
05-07 22:42:27.056 15552-16792/com.my.app V/Main Activity: Caught EVENT_CONNECT_ERROR
05-07 22:42:27.056 15552-16792/com.my.app V/Main Activity: Errors :: io.socket.engineio.client.EngineIOException: xhr poll error
05-07 22:42:32.061 15552-16957/com.my.app V/Main Activity: Caught EVENT_RECONNECTING
05-07 22:42:32.062 15552-16957/com.my.app V/Main Activity: Errors :: 13
因此,在尝试连接后,我立即收到xhr轮询错误,并且所有后续连接尝试都会遇到相同的结果。
我看到一些帖子说这样的问题可能是因为SSL证书问题造成的,尽管我用Socket.IO库使用的SSL上下文做的任何尝试都没有用。
如果任何人知道我可以尝试将其付诸实践,那就太棒了。
如果缺少任何信息或代码样例,请告诉我,我会添加它们。
答案 0 :(得分:6)
经过进一步测试后,我发现了以下内容:
我用来向套接字请求添加基本身份验证标头的代码:
// Adding authentication headers when encountering EVENT_TRANSPORT
mSocket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport) args[0];
// Adding headers when EVENT_REQUEST_HEADERS is called
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.v(TAG, "Caught EVENT_REQUEST_HEADERS after EVENT_TRANSPORT, adding headers");
Map<String, List<String>> mHeaders = (Map<String, List<String>>)args[0];
mHeaders.put("Authorization", Arrays.asList("Basic bXl1c2VyOm15cGFzczEyMw=="));
}
});
}
});
如果在尝试使用Socket.IO时遇到XHR轮询错误,请确保与套接字服务器的连接可用且连接正确。最后,在我的情况下,这一切都围绕着需要基本身份验证的服务器,而且在联系服务器时我没有提供它。
,因为我在尝试解决此问题时遇到了一些问题 - 尝试通过HTTPS访问您的服务器时,您通常不需要特殊主机名验证程序或特殊证书管理器。尽量避免使用此类解决方案,因为它们会极大地损害您应用的安全性。
答案 1 :(得分:3)
我可以根据自己的情况验证类似的问题。我正在测试与开发服务器的websocket连接。没有SSL,只有纯HTTP。这是我的情况:
"xhr poll error"
消息。然后,经过这么多时间没有成功,我在AndroidManifest.xml
中添加了以下内容:
<application
...
android:usesCleartextTraffic="true"
...>
我的项目当前针对的是API 28,因此"usesCleartextTraffic"
的默认值为false。现在一切正常。
答案 2 :(得分:1)
您必须在清单的application标签中添加以下代码:
<application
...
android:usesCleartextTraffic="true">
....
<application/>
答案 3 :(得分:0)
我使用了你的代码并且具有完全相同的输出,但你的分辨率并没有为我解决。我发现的解决方案是每X秒发出一次数据,如果没有数据被发送,基本上连接就会丢失。
我没有做任何授权。
代码与EVENT_TRANSPORT部分完全相同,EVENT_CONNECT如下:
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("mylogs", "Socket: Web socket opened");
Runnable runnable = new Runnable() {
@Override
public void run() {
socket.emit("YOUR_TOPIC", "YOUR_DATA_VALUE");
handler.postDelayed(this, 1000);
}
};
handler.post(runnable);
}
})
我只是想把它留在这里,它可能会帮助将来的某个人,不管你信不信,这是一个令人讨厌的问题,这个&#34; xhr轮询错误&#34;是非常模糊的,很多不同的东西都可以导致它。在尝试了很多不同的东西之后,这个简单的代码解决了它。
答案 4 :(得分:0)
如果您忘记在选项中设置运输方式,也会发生这种情况
IO.Options opts = new IO.Options();
opts.transports = new String[]{WebSocket.NAME}; //or Polling.NAME
答案 5 :(得分:0)
还要检查您的赠款
<uses-permission android:name="android.permission.INTERNET" />
在manifest.xml
还添加
<application
...
android:usesCleartextTraffic="true">
....
<application/>
答案 6 :(得分:0)
确保您的服务器接受 websocket 并在 trans 中进行轮询
global.io = new ioServer.Server(server, {
maxHttpBufferSize: 100000000,
connectTimeout: 5000,
transports:['websocket','polling'],
pingInterval: 25 * 1000,
pingTimeout: 5000,
allowEIO3: true,
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
}
})
然后禁用杀毒软件