Amit Kumar
Amit Kumar

Reputation: 407

Socket.io android not connecting it is working fine with web clinet not working in android

Hello I try to connect android my socket.io to my nodejs server which is hosted using ngnix

this is my android code which i have used in my android app

private var mSocket: Socket? = null
val opts = IO.Options()
opts.forceNew = true
opts.reconnection = true
val aara = arrayOf("websocket")
opts.transports = aara

try {
    mSocket = IO.socket(socket_url, opts)
    mSocket?.connect()
    mSocket?.emit("connection", "Nickname");
    mSocket?.emit("user_connect", "Nickname");
    Log.e("mSocket: ", mSocket?.connected().toString())
    mSocket?.on("connection") {
      Log.e("onCreate4w: ", "${it::class.simpleName} ${it.size}")
      Log.e("mSocket: ", mSocket?.connected().toString())
    }
} catch (e: URISyntaxException) {
    Log.e("onCreate25: ", mSocket?.connected().toString())
}

and server code is which is written in Nodejs

const io = soketio(server, {
  cors: {
    origin: [
      process.env.TYPEFORM_ORIGIN,
      process.env.ADMINPANEL_ORIGIN,
      
      "http://localhost:4200",
       "*"
      ,
    ],
    methods: ["GET", "POST"],
    allowedHeaders: ["my-custom-header"],
    credentials: true,
  },
  multiplex: false,
});
io.on("connection", (soket) => {
    soket.on("user_connect", (userId) => {
      // console.log("USER1", user);
      user[userId] = soket.id;
      // console.log("USER2", user);
      console.log("User Connected", soket.id);
    });

and this is ngnix configuration which are enabled in server

 location \ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://localhost:5555;
    }

Please guide us where we doing wrong this setup is working fine with web pannel, not in android

Upvotes: 0

Views: 190

Answers (1)

Stanislav Bondar
Stanislav Bondar

Reputation: 6245

At first mSocket?.connect() do not return connected socket and you not able to emit immediately. At second you need to subscribe on different socket states to handle errors

mSocket!!.on(io.socket.client.Socket.EVENT_CONNECT) {
        Log.d(TAG, "connected") // emit is possible
    }.on("connection") {
        Log.e("onCreate4w: ", "${it::class.simpleName} ${it.size}")
        Log.e("mSocket: ", mSocket?.connected().toString())
    }.on(io.socket.client.Socket.EVENT_CONNECT_ERROR) {
        it?.forEach { out ->
            Log.w("EVENT_CONNECT_ERROR", out.toString())
        }
    }.on(io.socket.client.Socket.EVENT_CONNECT_TIMEOUT) {
        it?.forEach { out ->
            Log.w("EVENT_CONNECT_TIMEOUT", out.toString())
        }
    }.on(io.socket.client.Socket.EVENT_RECONNECT_ERROR) {
        it?.forEach { out ->
            Log.w("EVENT_RECONNECT_ERROR", out.toString())
        }
    }.on(io.socket.client.Socket.EVENT_RECONNECT_FAILED) {
        it?.forEach { out ->
            Log.w("EVENT_RECONNECT_FAILED", out.toString())
        }
    }.on(io.socket.client.Socket.EVENT_CONNECTING) {
        it?.forEach { out ->
            Log.d("EVENT_CONNECTING", out.toString())
        }
    }
mSocket!!.connect()

Upvotes: 1

Related Questions