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