未捕获到WsException。
软件包版本已更新。 有package.json { “ @ nestjs / common”:“ ^ 6.0.0”, “ @ nestjs / core”:“ ^ 6.0.0”, “ @ nestjs / platform-socket.io”:“ ^ 6.6.7”, “ @ nestjs / websockets”:“ ^ 6.6.7” }
// game.gateway.ts
import {
WebSocketGateway,
SubscribeMessage,
WsException,
WsResponse,
OnGatewayConnection,
} from '@nestjs/websockets';
import { SocketExceptionFilter } from '../../filters/socket-exception.filter';
@WebSocketGateway(10000)
export class GameGateway implements OnGatewayConnection {
@SubscribeMessage('error')
@UseFilters(new SocketExceptionFilter())
handleError() {
throw new WsException('ws exception error');
}
}
// socket-exception.filter.ts
import { Catch, ArgumentsHost, WsExceptionFilter } from '@nestjs/common';
import { WsException } from '@nestjs/websockets';
@Catch(WsException)
export class SocketExceptionFilter implements WsExceptionFilter {
catch(exception: WsException, host: ArgumentsHost) {
const ctx = host.switchToWs();
const client: Client = ctx.getClient();
const data = ctx.getData();
console.log(data);
console.log(exception);
}
}
发出“错误”消息时,什么也没发生。