在android上使用Socket.IO总是返回XHR轮询错误

我正在编写一个需要连接到在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();

每当我尝试连接时(基本上是在line上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 poll错误,并且随后的所有连接尝试均达到相同的结果。

我看到一些帖子说这样的问题可能是由SSL证书问题引起的,尽管我对Socket.IO库使用的SSL上下文进行的任何尝试都无效。

如果anyoen有一个想法,我可能可以尝试使它起作用,那就太好了。

如果缺少任何信息或代码样本,请告诉我,我将其添加。

回答:

经过进一步测试,我发现以下内容:

  1. 如果在遇到其他事件(例如EVENT_ERROR)时调试套接字,则捕获的异常将包含从您尝试访问的地址收到的响应代码。
  2. 就我而言,当尝试使用HTTP到达服务器时,我收到了301响应代码,因为尝试访问的URL自动将我重定向到HTTPS地址。
  3. 尝试访问HTTPS地址时,我收到了401响应代码-表示我的套接字发出的请求已到达服务器,但未获得授权-原因是目标服务器已 而我没有提供套接字请求标头中的必需凭据。
  4. 提供所需的标头后,我设法正确连接到套接字服务器,但是在发送请求时失败,并且我的套接字返回到重新连接尝试循环-原因是我从服务器发送给服务器的JSON我的Android手机格式不符合预期,服务器无法收到该请求。

我用来向套接字请求添加基本身份验证标头的代码:

// 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访问服务器时,通常

特殊的主机名验证程序或特殊的证书管理器。尝试避免使用此类解决方案,因为它们会严重损害您的应用程序的安全性。

以上是 在android上使用Socket.IO总是返回XHR轮询错误 的全部内容, 来源链接: utcz.com/qa/414476.html

回到顶部