Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io Android HTTPS - Connection Error xhr poll error

I'm developing a chat application with HTTPS. This is the Socket.IO I've used

Server

First of all I developed the server with node.js and node module "socket.io": "^1.4.5"

In the server I have this:

var secure = {
  ca: fs.readFileSync('ca.pem'),
  key: fs.readFileSync('key.key'),
  cert: fs.readFileSync('cert.crt')
};

var server = https.createServer(secure, app);
var io = require('socket.io')(server);

Clients

I developed the website with https://cdn.socket.io/socket.io-1.4.5.js

I developed the iOS application with pod 'Socket.IO-Client-Swift', '~> 5.5.0'

Finally I'm developing the Android application with

compile ('io.socket:socket.io-client:0.7.0') {
  exclude group: 'org.json', module: 'json'
}

In the website and in iOS application I haven't problems with the websocket, but when I try to connect the socket in Android application I receive the following error:

io.socket.engineio.client.EngineIOException: xhr poll error

Thanks

like image 318
rmh Avatar asked Nov 21 '22 11:11

rmh


1 Answers

If you're trying to connect through HTTPS and use Nginx as a reverse proxy on the backend, make sure to configure the upgrade options correctly:

upstream nodes {
  # enable sticky session based on IP
  ip_hash;
  # Backend nodejs server
  server 127.0.0.1:5500;
}


server {
  listen 80;
  # The host name to respond to
  server_name mysite.com;


  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;


    proxy_pass http://nodes;


    # enable WebSockets
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
} 

This worked for me in solving this mystic error.

Source: https://nhancv.com/android-socket-io-client-v1-0-0-engineioexception-xhr-poll-error/

like image 124
Thijs Avatar answered Dec 12 '22 20:12

Thijs