【问题标题】:How to emit and recieve whitout a Authenticated using socketio-jwt如何在没有经过身份验证的情况下使用套接字 io-jwt 发送和接收
【发布时间】:2017-03-02 13:56:26
【问题描述】:

当客户端拥有令牌(由 Laravel jwt 提供)时,我的套接字可以正常工作,但是需要在客户端尚未经过身份验证时与它们一起工作,我想要类似的东西:

io.sockets
.on('connection', socketioJwt.authorize({
  secret: process.env.JWT_SECRET,
  timeout: 15000 // 15 seconds to send the authentication message
}))

.on('authenticated', function(socket) {
  console.log('Authenticated!!');
  // This works:
  socket.on('setDataRegistered', function(datos) {
    console.log('Registering!');
  });
})
// This doesn't works:
.on('config', function(socket) {
  console.log('A client wants to be configured before authenticated!');
})

如何在验证之前从前端调用“config”(socket.emit('config'))?

感谢您的帮助。对不起我的英语。

【问题讨论】:

  • 你好,谁能帮帮我。

标签: node.js sockets socket.io jwt


【解决方案1】:

我的做法是这样的:

io.on('connection', function (socket) {
        // Client connected but not authenticated. Client has to emit to 'authenticate' for authentication

        socket.on('authenticate', function (data, fn) {
            // Authenticate socket.

            // define more events
        });

        // still accessible to unauthorised sockets
        socket.on("other-event", function(data) {

        });
});

我不使用任何中间件进行身份验证。但这只是我。我从来没有发现它的用途。

【讨论】:

  • 我是这么想的。但是如何在“身份验证”函数中验证 jwt 套接字?这就是中间件为我做的事情。那么,我该如何手动完成呢?谢谢!
  • 我使用 Laravel 来验证和创建令牌。稍后,我将该令牌发送给 nodejs。
  • @bluesky777 你只需解码令牌。检查您图书馆的文档。
  • @bluesky777 很高兴我能帮上忙
  • 我花了两个小时试图弄清楚为什么我的客户端没有向服务器发送发射。原来我使用的是io.on 而不是socket.on,但服务器需要使用socket 检查事件。如果遇到类似问题,请确保您使用的是套接字!
猜你喜欢
  • 2016-04-30
  • 2020-12-04
  • 2022-01-03
  • 2016-08-15
  • 1970-01-01
  • 2019-05-22
  • 2012-04-03
  • 2012-01-11
  • 2018-01-04
相关资源
最近更新 更多