我為用戶身份驗證編寫套接字中間件,但我不知道我的問題是什么!在 android 端“未經授權”事件未調用。服務器端 :async function (socket, next) { if (socket.handshake.query && socket.handshake.query.token) { jwt.verify(socket.handshake.query.token, process.env.SECRET_JWT_KEY, function (err, decoded) { if (err) { socket.emit( "unauthorized", "unauthorized user") <==send to sender you'r authentication is failed console.log("Query: ", "unauthorized"); <==This line is lunched return next(new Error('Authentication error')); } socket.decoded = decoded; next(); }); } else { next(new Error('Authentication error')); }客戶端 : val socket = IO.socket("http://192.168.1.6:3000", IO.Options().apply { path = "/sockets/chat" query = "token=wrongtoken" }) socket.on("unauthorized", object : Emitter.Listener { <== THIS NOT LUNCED!! override fun call(vararg args: Any?) { println("user authentication failed" ) } }
為什么不調用 android 端的 socket.io 偵聽器?
鴻蒙傳說
2023-06-15 15:40:40