Using Socket.IO on android Always Returns XHR Poll Error

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I am writing an android app that needs to connect to a Socket.IO instance running on a node.js server.

Attempting to connect to the instance and transmitting data using an iOS device works flawlessly, but when I try to do it with an android device it fails.

I am using the Native java client, and this is the code I'm using on the android side:

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(); 

Whenever I try to connect (Basically as soon as the line mSocket.connect();) The log prints the following lines:

So I immediately get an xhr poll error after attempting to connect and all following connection attempts meet the same result.

I saw some posts saying such an issue might be cause by an SSL certificate issue though any attempts I made to temper with the SSL Context the Socket.IO library uses didn't work.

If anyoen has an idea what I might be able to try to get this to work it would be excellent.

If any information or code sampels are missing, let me know and I'll add them.

回答1:

After further testing I found the following:

  1. If you debug your socket when you encounter different events (i.e. EVENT_ERROR), the exception you catch will contain the response code received from the address you tried to reach.
  2. In my case when attempting to reach my server with HTTP I received a 301 response code because the URL I tried reaching automatically redirected me to the HTTPS address.
  3. When attempting to reach the HTTPS address I received a 401 response code - meaning the request my socket made had reached the server but was not authorized - The reason for that was that the target server had basic authentication turned on and I didn't supply the required credentials in the socket request headers.
  4. After supplying the required headers I managed to connect to the socket server properly, but when sending out a request it failed and my socket went back to a re-connection attempt loop - The reason for that was that the JSON I sent to the server from my Android phone was not in the format expected and the server failed to receive that request.

The code I used to add a basic authentication header to my socket request:

// 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=="));             }         });     } }); 

If you encounter and XHR Poll error when you attempt to use Socket.IO, make sure that the connection to your socket server is available and that you are connecting properly. In the end in my case it all revolved around the server requiring basic authentication and me not supplying it when contacting the server.

Also, because I encountered that solution qutie a bit while trying to sovle this issue - When trying to reach your server over HTTPS, you usually don't need a special hostname verifier or special certificate manager. Try and avoid such solutions as they greatly harm the security of your app.



回答2:

i used your code and was having the exact same output, but your resolution didn't solve it for me. The solution i found was to emit data every X seconds, basically the connection dropped if there was no data being sent.

I didn't had to do any authorization.

The code is exactly as above the EVENT_TRANSPORT part and the EVENT_CONNECT is as follows:

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);              }         }) 

I just wanted to leave this here, it might help someone in the future, believe it or not, this was a nasty one to solve, the "xhr poll error" is very vague and a lot of different things can lead to it. After trying so many different things, this simple code solved it.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!