-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwss.gateway.ts
More file actions
executable file
·71 lines (58 loc) · 1.85 KB
/
wss.gateway.ts
File metadata and controls
executable file
·71 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
OnGatewayConnection,
OnGatewayDisconnect,
WebSocketGateway,
WebSocketServer,
MessageBody,
SubscribeMessage,
ConnectedSocket,
} from '@nestjs/websockets';
import io from 'socket.io';
import { NextFunction } from 'express';
import Redis from 'ioredis';
import { Server, ServerOptions, Socket } from 'socket.io';
import { LoggerService } from '../common/logger/logger.service';
//const wss_settings = config.get<IWssSettings>('WSS_SETTINGS');
/*
@WebSocketGateway(8081, {
pingInterval: 3000,
pingTimeout: 10000,
path: '/ws',
})*/
@WebSocketGateway(8083, {
pingInterval: 3000,
pingTimeout: 10000,
path: '/ws',
transport: ['websocket'],
})
export class WssGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
wss;
public server: io.Server;
constructor(private readonly logger: LoggerService) {}
private getClientQuery(client: io.Socket): { [key: string]: string } {
return client.handshake.query as { [key: string]: string };
}
public broadcastAll(event_name: string, message: Record<string, unknown>) {
this.server.emit(event_name, message);
}
handleConnection(client: io.Socket) {
client.join('msgRoom');
const { user_id } = this.getClientQuery(client);
this.logger.info(`WssGateway: handleConnection ${user_id}`);
return this.broadcastAll('event', { connected: user_id });
}
handleDisconnect(client: io.Socket) {
const { user_id } = this.getClientQuery(client);
this.logger.info(`WssGateway: handleDisconnect ${user_id}`);
return this.broadcastAll('event', { disconnected: user_id });
}
@SubscribeMessage('send_message')
listenForMessages(@MessageBody() content: string, @ConnectedSocket() socket: Socket) {
const { user_id } = this.getClientQuery(socket);
this.server.sockets.emit('receive_message', {
content,
user_id,
});
}
}