Socket.io android未连接它工作正常,web clinet未在android中工作

Socket.io android未连接它工作正常,web clinet未在android中工作,android,nginx,websocket,socket.io,Android,Nginx,Websocket,Socket.io,您好,我尝试将android my socket.io连接到使用ngnix托管的nodejs服务器 这是我在android应用程序中使用的android代码 private var mSocket: Socket? = null val opts = IO.Options() opts.forceNew = true opts.reconnection = true val aara = arrayOf("websocket") opts.transports = aara

您好,我尝试将android my socket.io连接到使用ngnix托管的nodejs服务器

这是我在android应用程序中使用的android代码

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())
}
服务器代码是用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);
    });
这是在服务器中启用的ngnix配置

 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;
    }
请指导我们哪里做错了
此设置在web pannel中运行良好,而不是在android中。首先
mSocket?.connect()
不返回已连接的套接字,您无法立即发射。第二步,您需要订阅不同的套接字状态来处理错误

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

你好,斯坦尼斯拉夫。感谢您的回复,但这种方法也不适用working@AmitKumarLogcat没有显示任何日志?我不认为这是android的问题,这是更多的节点问题,因为同样的事情发生在Ios上part@AmitKumar使用默认设置尝试并删除
opts.transports
overrideHello Stanislav,感谢您宝贵的时间。您的代码帮助我们获得导致套接字未连接的实际问题。节点端存在版本控制问题。我们现在在后端使用2.3.0版本,现在我的代码运行良好