为了账号安全,请及时绑定邮箱和手机立即绑定

为什么不调用 android 端的 socket.io 侦听器?

为什么不调用 android 端的 socket.io 侦听器?

鸿蒙传说 2023-06-15 15:40:40
我为用户身份验证编写套接字中间件,但我不知道我的问题是什么!在 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" )                }            }
查看完整描述

2 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

终于找到了解决方案,在抛出错误时无需从中间件发出,您可以在 Android 客户端中获取预定义事件,如下所示:

socket.on(Socket.EVENT_ERROR) { args -> println(args.contentToString()) }


查看完整回答
反对 回复 2023-06-15
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

我认为您需要先通过在 apply 块中调用这一行来连接到套接字

this.connect()


查看完整回答
反对 回复 2023-06-15
  • 2 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信