Socket.io NestJs套接字身份验证,在handleConnection中发出事件

Socket.io NestJs套接字身份验证,在handleConnection中发出事件,socket.io,nestjs,nestjs-gateways,Socket.io,Nestjs,Nestjs Gateways,身份验证失败后,在断开连接之前,我想向客户端发出一个事件,通知请求提供的令牌无效。在我的WebSocketGateway中,这是我的代码: handleConnection(client: Socket, ...args: any[]) { try { this.socketAuthService.authenticate(client); this.logger.log(`Client connected: ${client.i

身份验证失败后,在断开连接之前,我想向客户端发出一个事件,通知请求提供的令牌无效。在我的WebSocketGateway中,这是我的代码:

 handleConnection(client: Socket, ...args: any[]) {
        try {
            this.socketAuthService.authenticate(client);
            this.logger.log(`Client connected: ${client.id}`);
        } catch (e) {
            this.logger.error(`Client ${client.id} tried to connect with invalid token`);
            client.emit('unauthorized', 'Invalid Token');
            client.disconnect();
            this.logger.log(`Client disconnected due to invalid token: ${client.id}`);
        }
    }
我的客户:

this.socket.on('unauthorized', (msg) => {
  console.log(msg);
});
未发送/接收事件。有什么想法吗


干杯

socketAuthService。身份验证
同步?是的,是同步的。你使用了警卫吗。或控制台所有
客户端由于无效令牌而断开连接:
可能是您的客户端在函数handleConnection运行之前断开连接,直到我断开客户端连接。事件根本不会被发出。我使用allowFunction()对问题进行了排序。我更喜欢这个想法,因为它甚至不让套接字连接,我不喜欢的是,它不允许我抛出401,而是发送一个CORS错误