簡體   English   中英

無法在 android kotlin 中的 socket.io 中接收來自服務器的確認

[英]Not able to receive acknowledgement from server in socket.io in android kotlin

我成功地在客戶端服務器之間建立了連接,但問題是我的一個事件chat:enter無法從 android 中的服務器確認,它在前端工作正常,只是在 android 中出現問題。

用於套接字連接

 fun connectWithChatServer(message: Message, onSocketIOEventListener: Listeners.OnSocketIOEventListener) {
    try {
        if (message.chatConnectionInfo != null) {
            val token: String = message.chatConnectionInfo?.token!!
            try {
                val opts = IO.Options()
                opts.forceNew = true
                opts.query = "{token : '$token'}"
                opts.transports = arrayOf("websocket")
                opts.path = "/chatsignal"
                mSocket = IO.socket(message.chatConnectionInfo?.connectionUrl.toString(), opts)
                makeConnection()
            } catch (e: Exception) {
                e.printStackTrace()
                Log.d("fail", "Failed to connect")
            }
        }
    } catch (e: URISyntaxException) {
        throw RuntimeException(e)
    }
}
private fun makeConnection() {
    if (mSocket != null) {
        mSocket?.connect()
        registerConnectionAttributes()
    }
}
private fun registerConnectionAttributes() {
    try {
        if (mSocket != null) {
            mSocket?.on(Socket.EVENT_CONNECT, onConnect)
           
        }
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
}

onConnect 發射器

private var onConnect = Emitter.Listener {
        mSocket!!.emit("chat:enter", "{}", Ack { args ->
            Constants.printDebug(logTag, "Ack ${args[0]}")
            val text = args[0].toString()
        })
    }

你必須在連接之前注冊一個監聽器

所以你的makeConnection應該改為

private fun makeConnection() {
    if (mSocket != null) {
        registerConnectionAttributes()
        mSocket?.connect()
    }
}
if (message.chatConnectionInfo != null) {
        val token: String = message.chatConnectionInfo?.token!!
        try {
            val opts = IO.Options()
            opts.forceNew = true
            opts.query = "token=$token"
            opts.transports = arrayOf("websocket")
            opts.path = "/chatsignal"
            mSocket = IO.socket(message.chatConnectionInfo?.connectionUrl.toString(), opts)
            makeConnection()
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("fail", "Failed to connect")
        }
    }

我只需在connectWithChatServer() opts.query = "{token : '$token'}"更改為opts.query = "token=$token"即可解決

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM