[英]Not able to receive acknowledgement from server in socket.io in android kotlin
I successfully make connection between client server but problem is that one of my event chat:enter
cannot be acknowledge from server in android and it is working fine in frontend , just problem in android.我成功地在客户端服务器之间建立了连接,但问题是我的一个事件
chat:enter
无法从 android 中的服务器确认,它在前端工作正常,只是在 android 中出现问题。
for socket connection用于套接字连接
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 emitter onConnect 发射器
private var onConnect = Emitter.Listener {
mSocket!!.emit("chat:enter", "{}", Ack { args ->
Constants.printDebug(logTag, "Ack ${args[0]}")
val text = args[0].toString()
})
}
you have to register a listener before connecting你必须在连接之前注册一个监听器
so your makeConnection
should be changed to that所以你的
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")
}
}
I solved by just need to change opts.query = "{token : '$token'}"
to opts.query = "token=$token"
in connectWithChatServer()
我只需在
connectWithChatServer()
opts.query = "{token : '$token'}"
更改为opts.query = "token=$token"
即可解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.