Implement OnGatewayInit and OnGatewayDisconnect
parent
2ef8c15f63
commit
96b9eac4f7
|
@ -6,6 +6,8 @@ import {
|
||||||
import { Logger } from '@nestjs/common';
|
import { Logger } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
OnGatewayConnection,
|
OnGatewayConnection,
|
||||||
|
OnGatewayDisconnect,
|
||||||
|
OnGatewayInit,
|
||||||
SubscribeMessage,
|
SubscribeMessage,
|
||||||
WebSocketGateway,
|
WebSocketGateway,
|
||||||
WebSocketServer,
|
WebSocketServer,
|
||||||
|
@ -14,7 +16,8 @@ import { AppService } from './app.service';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
|
||||||
@WebSocketGateway(4001)
|
@WebSocketGateway(4001)
|
||||||
export class AppGateway implements OnGatewayConnection {
|
export class AppGateway
|
||||||
|
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
|
||||||
@WebSocketServer()
|
@WebSocketServer()
|
||||||
wss: Server;
|
wss: Server;
|
||||||
|
|
||||||
|
@ -22,10 +25,19 @@ export class AppGateway implements OnGatewayConnection {
|
||||||
|
|
||||||
constructor(private appService: AppService) {}
|
constructor(private appService: AppService) {}
|
||||||
|
|
||||||
|
afterInit(server: Server) {
|
||||||
|
this.logger.log('Initialized...');
|
||||||
|
}
|
||||||
|
|
||||||
handleConnection(client: Socket, ...args: any[]) {
|
handleConnection(client: Socket, ...args: any[]) {
|
||||||
|
this.logger.log(`Client connected: ${client.id}`);
|
||||||
client.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());
|
client.emit(MESSAGE_TO_CLIENT, this.appService.getMessages());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleDisconnect(client: Socket) {
|
||||||
|
this.logger.log(`Client disconnected: ${client.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeMessage(MESSAGE_TO_SERVER)
|
@SubscribeMessage(MESSAGE_TO_SERVER)
|
||||||
handleMessage(client: Socket, payload: Message): void {
|
handleMessage(client: Socket, payload: Message): void {
|
||||||
this.logger.log(payload);
|
this.logger.log(payload);
|
||||||
|
|
Loading…
Reference in New Issue