51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
import {
|
|
MessageBody,
|
|
OnGatewayConnection,
|
|
SubscribeMessage,
|
|
WebSocketGateway,
|
|
WebSocketServer,
|
|
} from '@nestjs/websockets';
|
|
import { Server, Socket } from 'socket.io';
|
|
import { MessagesEventService } from './messages-event.service';
|
|
|
|
@WebSocketGateway({
|
|
cors: {
|
|
origin: '*',
|
|
},
|
|
})
|
|
export class MessagesGateway implements OnGatewayConnection {
|
|
@WebSocketServer()
|
|
server: Server;
|
|
|
|
constructor(private event: EventEmitter2) {}
|
|
|
|
async onModuleInit() {
|
|
const eventsToForward = [
|
|
MessagesEventService.EVENTS.GET_CONVERSATIONS,
|
|
MessagesEventService.EVENTS.GET_CONVERSATION,
|
|
MessagesEventService.EVENTS.SEND_MESSAGE,
|
|
MessagesEventService.EVENTS.REPLY_MESSAGE,
|
|
];
|
|
|
|
for (const event of eventsToForward) {
|
|
this.event.on(event, (data) => {
|
|
this.server.emit(event, data);
|
|
});
|
|
}
|
|
}
|
|
async handleConnection(client: Socket) {
|
|
console.log(`📢 Client connected: ${client.id}`);
|
|
}
|
|
|
|
@SubscribeMessage(MessagesEventService.EVENTS.RECEIVE_CONVERSATIONS)
|
|
async handleReiveConversations(@MessageBody() data: any) {
|
|
this.event.emit(MessagesEventService.EVENTS.RECEIVE_CONVERSATIONS, data);
|
|
}
|
|
|
|
@SubscribeMessage(MessagesEventService.EVENTS.RECEIVE_CONVERSATION)
|
|
async handleReiveConversation(@MessageBody() data: any) {
|
|
this.event.emit(MessagesEventService.EVENTS.RECEIVE_CONVERSATION, data);
|
|
}
|
|
}
|