2020-10-09 16:38:55 +00:00
|
|
|
import type { Message } from '@chat-room/api-interfaces';
|
2020-10-09 16:21:21 +00:00
|
|
|
import {
|
|
|
|
MESSAGE_TO_CLIENT,
|
|
|
|
MESSAGE_TO_SERVER,
|
|
|
|
} from '@chat-room/api-interfaces';
|
|
|
|
import { Logger } from '@nestjs/common';
|
|
|
|
import {
|
|
|
|
OnGatewayConnection,
|
|
|
|
SubscribeMessage,
|
|
|
|
WebSocketGateway,
|
|
|
|
WebSocketServer,
|
|
|
|
} from '@nestjs/websockets';
|
|
|
|
import { Server } from 'http';
|
|
|
|
import { AppService } from './app.service';
|
|
|
|
|
|
|
|
@WebSocketGateway(4001)
|
|
|
|
export class AppGateway implements OnGatewayConnection {
|
|
|
|
@WebSocketServer()
|
|
|
|
wss: Server;
|
|
|
|
|
|
|
|
private logger: Logger = new Logger('AppGateway');
|
|
|
|
|
|
|
|
constructor(private appService: AppService) {}
|
|
|
|
|
|
|
|
handleConnection(client: any, ...args: any[]) {
|
|
|
|
client.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeMessage(MESSAGE_TO_SERVER)
|
|
|
|
handleMessage(client: any, payload: Message): void {
|
|
|
|
this.logger.log(payload);
|
|
|
|
this.appService.addMessage(payload);
|
|
|
|
this.wss.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());
|
|
|
|
}
|
|
|
|
}
|