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 us
This will also happen if you forgot to set transport in options
IO.Options opts = new IO.Options();
opts.transports = new String[]{WebSocket.NAME}; //or Polling.NAME
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.
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.
You have to add this code in application tag in the manifest as below :
<application
...
android:usesCleartextTraffic="true">
....
<application/>
I can verify similar issue with my situation. I am testing websocket connection to a development server. No SSL, just plain http. Here was my situation:
"xhr poll error"
message.Then, after so much time with no success, I added in AndroidManifest.xml
the following:
<application
...
android:usesCleartextTraffic="true"
...>
My project is currently targeting API 28, hence default for "usesCleartextTraffic"
is false. Now everything is working fine.
Also check whether your grant
<uses-permission android:name="android.permission.INTERNET" />
in manifest.xml
also add
<application
...
android:usesCleartextTraffic="true">
....
<application/>