与新的套接字对象重新连接时,SocketIO断开连接并出现错误xhr poll错误

时间:2020-06-21 11:40:30

标签: android socket.io

我正在尝试使用android中的socket.IO实现即时聊天。 当我第一次连接时,它的连接没有任何错误,但是当我断开网络并重新连接时,出现了一个错误 EngineIOException:xhr poll error

这是我的代码


        fun connect(token: String) {
            val userId = account.userId
            if (socket != null && socket!!.connected()) {
                Log.d(TAG, "Already connected...")
                return
            }
    
            isConnecting = true
            Log.d(TAG, "connecting...: " )
            val options = IO.Options()
            options.query = "userId=$userId"
            options.reconnection = false
            options.forceNew = true
         //   options.transports = arrayOf<String>(WebSocket.NAME); //or Polling.NAME
          //  options.upgrade =false
            initEvents()
            socket?.connect()
        }
    
    private fun initEvents(){
      socket?.on(Socket.EVENT_CONNECT) {
                Log.d(TAG, "socket.io connected")
                App.get().runOnUiThread {
                    isConnecting = false
                    linkState.state = LinkState.ONLINE
                    /* JobSingleton.getInstance().jobManager.start()
                     socketObserver?.onOpen()*/
                }
            }
    
            socket?.on(Socket.EVENT_ERROR) {
                Log.e(TAG, "SOCKET ERROR : " + (it as Array)[0].toString())
            }
    
            socket?.on(Socket.EVENT_CONNECT_TIMEOUT) {
                isConnecting = false
                Log.e(TAG, "SOCKET CONNECTION TIMEOUT")
                socketObserver?.onClose()
            }
    
            socket?.on(Socket.EVENT_DISCONNECT) {
                Log.d(TAG, "SOCKET DISCONNECTED")
    
                isConnected = false
                socket?.disconnect()
                socket?.off()
                socket = null
                socketObserver?.onClose()
    
                App.get().runOnUiThread {
                    linkState.state = LinkState.OFFLINE
                }
            }
    }

这是日志

//第一次连接 D / SocketManager:正在连接...: D / SocketManager:已连接socket.io

//关闭互联网 E / SocketManager:套接字错误:io.socket.engineio.client.EngineIOException:websocket错误 D / SocketManager:插座已断开

//再次打开互联网 D / SocketManager:正在连接...: D / SocketManager:已连接socket.io E / SocketManager:套接字错误:io.socket.engineio.client.EngineIOException:xhr轮询错误 D / SocketManager:插座断开 D / SocketManager:正在连接...:

0 个答案:

没有答案