2 Commits

1 changed files with 16 additions and 4 deletions
Split View
  1. +16
    -4
      apps/api/src/app/app.gateway.ts

+ 16
- 4
apps/api/src/app/app.gateway.ts View File

@@ -6,15 +6,18 @@ import {
import { Logger } from '@nestjs/common';
import {
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { Server } from 'http';
import { AppService } from './app.service';
import { Server, Socket } from 'socket.io';

@WebSocketGateway(4001)
export class AppGateway implements OnGatewayConnection {
export class AppGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
wss: Server;

@@ -22,12 +25,21 @@ export class AppGateway implements OnGatewayConnection {

constructor(private appService: AppService) {}

handleConnection(client: any, ...args: any[]) {
afterInit(server: Server) {
this.logger.log('Initialized...');
}

handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client connected: ${client.id}`);
client.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());
}

handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`);
}

@SubscribeMessage(MESSAGE_TO_SERVER)
handleMessage(client: any, payload: Message): void {
handleMessage(client: Socket, payload: Message): void {
this.logger.log(payload);
this.appService.addMessage(payload);
this.wss.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());


Loading…
Cancel
Save