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:
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
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.
Websockets is the persistent connection that can be used to receive/send data without sequential order and without http header. Xhr-polling creates new request with http header and waits for answer with http header, also sequential order.
Socket.IO typically uses WebSockets as its transport layer, but sometimes uses HTTP "long-polling" as a fallback when WebSockets can't be used.
socketio-auth implements two-step authentication: upon connection, the server marks the clients as unauthenticated and listens to an authentication event. If a client provides wrong credentials or doesn't authenticate after a timeout period it gets disconnected.
After further testing I found the following:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With