From 42cf50ee7521bd751f4d0f0798276e548bb83fee Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Wed, 30 Mar 2022 22:15:20 -0600 Subject: Working --- server/providers/gateways/chat_room.gateway.ts | 67 ++++++++++++++++++++++++++ server/providers/gateways/ping.gateway.ts | 67 -------------------------- 2 files changed, 67 insertions(+), 67 deletions(-) create mode 100644 server/providers/gateways/chat_room.gateway.ts delete mode 100644 server/providers/gateways/ping.gateway.ts (limited to 'server/providers/gateways') diff --git a/server/providers/gateways/chat_room.gateway.ts b/server/providers/gateways/chat_room.gateway.ts new file mode 100644 index 0000000..b565d40 --- /dev/null +++ b/server/providers/gateways/chat_room.gateway.ts @@ -0,0 +1,67 @@ +import { UseGuards } from '@nestjs/common'; +import { + ConnectedSocket, + MessageBody, + OnGatewayConnection, + OnGatewayDisconnect, + OnGatewayInit, + SubscribeMessage, + WebSocketGateway, + WebSocketServer, + WsException, +} from '@nestjs/websockets'; +import { GatewayJwtBody } from 'server/decorators/gateway_jwt_body.decorator'; +import { JwtBodyDto } from 'server/dto/jwt_body.dto'; +import { Server, Socket } from 'socket.io'; +import { GatewayAuthGuard } from '../guards/gatewayauth.guard'; +import { JwtService } from '../services/jwt.service'; +import { UsersService } from '../services/users.service'; + +@WebSocketGateway() +@UseGuards(GatewayAuthGuard) +export class ChatRoomGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect { + @WebSocketServer() + server: Server; + + constructor(private jwtService: JwtService, private userService: UsersService) {} + + afterInit(server: Server) { + console.log('Sockets initialized'); + } + + handleConnection(client: Socket) { + // you can do things like add users to rooms + // or emit events here. + // IMPORTANT! The GatewayAuthGuard doesn't trigger on these handlers + // if you need to do anything in this method you need to authenticate the JWT + // manually. + try { + const jwt = client.handshake.auth.token; + const jwtBody = this.jwtService.parseToken(jwt); + const chatRoomId = client.handshake.query.chatRoomId; + console.log('Client Connected: ', jwtBody.userId); + client.join(chatRoomId); + } catch (e) { + throw new WsException('Invalid token'); + } + } + + handleDisconnect(client: Socket) { + console.log('Client Disconnected'); + } + + @SubscribeMessage('message') + public async handleMessage( + @ConnectedSocket() client: Socket, + @MessageBody() data: string, + @GatewayJwtBody() jwtBody: JwtBodyDto, + ) { + const user = await this.userService.find(jwtBody.userId); + this.server.to(client.handshake.query.chatRoomId).emit('new-message', { + id: user.id * Math.random() * 2048 * Date.now(), + content: data, + userName: `${user.firstName} ${user.lastName}`, + userId: user.id, + }); + } +} diff --git a/server/providers/gateways/ping.gateway.ts b/server/providers/gateways/ping.gateway.ts deleted file mode 100644 index 27fe785..0000000 --- a/server/providers/gateways/ping.gateway.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { UseGuards } from '@nestjs/common'; -import { ConnectedSocket, MessageBody, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, SubscribeMessage, WebSocketGateway, WebSocketServer, WsException } from '@nestjs/websockets'; -import { GatewayJwtBody } from 'server/decorators/gateway_jwt_body.decorator'; -import { JwtBodyDto } from 'server/dto/jwt_body.dto'; -import { Server, Socket } from 'socket.io'; -import { GatewayAuthGuard } from '../guards/gatewayauth.guard'; -import { JwtService } from '../services/jwt.service'; - -class JoinPayload { - currentRoom?: string; - newRoom: string; -} - -class PingPayload { - currentRoom: string; -} - -@WebSocketGateway() -@UseGuards(GatewayAuthGuard) -export class PingGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect { - @WebSocketServer() - server: Server; - - constructor(private jwtService: JwtService) {} - - afterInit(server: Server) { - console.log('Sockets initialized'); - } - - handleConnection(client: Socket) { - // you can do things like add users to rooms - // or emit events here. - // IMPORTANT! The GatewayAuthGuard doesn't trigger on these handlers - // if you need to do anything in this method you need to authenticate the JWT - // manually. - try { - const jwt = client.handshake.auth.token; - const jwtBody = this.jwtService.parseToken(jwt); - console.log(client.handshake.query); - console.log('Client Connected: ', jwtBody.userId); - } catch (e) { - throw new WsException('Invalid token'); - } - } - - handleDisconnect(client: Socket) { - console.log('Client Disconnected'); - } - - @SubscribeMessage('ping') - public handlePing( - @ConnectedSocket() client: Socket, - @MessageBody() payload: PingPayload, - @GatewayJwtBody() jwtBody: JwtBodyDto, - ) { - this.server.to(payload.currentRoom).emit('pong', { message: { userId: jwtBody.userId } }); - console.log(client.rooms); - } - - @SubscribeMessage('join-room') - public async joinRoom(client: Socket, payload: JoinPayload) { - console.log(payload); - payload.currentRoom && (await client.leave(payload.currentRoom)); - await client.join(payload.newRoom); - return { msg: 'Joined room', room: payload.newRoom }; - } -} -- cgit v1.2.3-70-g09d2