【问题标题】:Socket.io android not connecting it is working fine with web clinet not working in androidSocket.io android 未连接它与 Web 客户端无法在 android 中正常工作
【发布时间】:2021-01-13 04:04:30
【问题描述】:

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

这是我在我的安卓应用中使用的安卓代码

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 面板,而不是在 android 中

【问题讨论】:

    标签: android nginx websocket socket.io


    【解决方案1】:

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

    【讨论】:

    • 你好斯坦尼斯拉夫。感谢您的回复,但这种方法也不起作用
    • @AmitKumar Logcat 没有显示任何日志?
    • 我不认为这是 android 问题,这是更多节点问题,因为同样的事情发生在 Ios 部分
    • @AmitKumar 尝试使用默认值并删除 opts.transports 覆盖
    • 您好 Stanislav,感谢您宝贵的时间。您的代码帮助我们解决导致套接字未连接的实际问题。节点端存在版本控制问题。我们现在在后端使用 2.3.0 版本,现在我的代码工作正常
    猜你喜欢
    • 2019-04-05
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多