Browse Source

Implement OnGatewayInit and OnGatewayDisconnect

master
Kevin C. Coram 1 year ago
parent
commit
96b9eac4f7
Signed by: kevin GPG Key ID: 0E64ECB9EDE572A3
1 changed files with 14 additions and 2 deletions
  1. +14
    -2
      apps/api/src/app/app.gateway.ts

+ 14
- 2
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 { AppService } from './app.service';
import { Server,Socket } from 'socket.io';
import { Server, Socket } from 'socket.io';

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

@@ -22,10 +25,19 @@ export class AppGateway implements OnGatewayConnection {

constructor(private appService: AppService) {}

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: Socket, payload: Message): void {
this.logger.log(payload);


Loading…
Cancel
Save