如何使用socketio-jwt发出和接收经过身份验证的消息

blu*_*777 5 sockets node.js socket.io jwt

当客户端具有令牌(由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!');
})
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助。对不起,我的英语。

Abh*_*ain 4

我所做的是这样的:

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) {

        });
});
Run Code Online (Sandbox Code Playgroud)

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