NestJs 套接字身份验证,在 handleConnection

时间:2021-05-03 19:41:58

标签: socket.io nestjs nestjs-gateways

在身份验证失败后,在断开连接之前,我想向客户端发出一个事件,通知请求提供的令牌无效。在我的 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);
});

未发送/接收事件。有什么想法吗?

干杯

0 个答案:

没有答案
相关问题