У меня есть events.gateway.ts , который контролирует все события клиента и сервера, но когда я делаю this.server.emit('client.events', data)
из @EventPattern('gw-event:playback.state')
, у меня emit null. Пожалуйста, смотрите ниже код:
import { Logger, Inject, Controller } from '@nestjs/common';
import { SubscribeMessage,
WebSocketGateway,
MessageBody,
WebSocketServer,
OnGatewayInit,
OnGatewayConnection,
OnGatewayDisconnect} from '@nestjs/websockets';
import { Socket, Server } from 'socket.io';
import { ClientProxy, EventPattern} from '@nestjs/microservices';
@Controller('gateway')
@WebSocketGateway()
export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect{
@WebSocketServer() server: Server;
private logger: Logger = new Logger('EventsGateway');
constructor(@Inject('EVENTS_SERVICE') private readonly client: ClientProxy) {
}
afterInit(server: Server) {
this.server = server;
this.logger.log('Init', EventsGateway.name);
}// OnGatewayInit
handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`, EventsGateway.name);
}// OnGatewayDisconnect
handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client connected: ${client.id}`, EventsGateway.name);
this.server.emit('client.events', 'Welcome client:' + client.id);
}// OnGatewayConnection
@SubscribeMessage('client.events')
handleEvent(@MessageBody() data: string): any {
return { event: 'client.events', 'data': data + '__from Server'};
}
@EventPattern('gw-event:playback.state')
async handlePlaybackState(data: Record<string, unknown>) {
this.server.emit('client.events', data); //<= Error emit is null
}
}
Есть предложения? Благодаря.