|
1 | | -import { UnauthorizedException } from '@nestjs/common'; |
2 | 1 | import { JwtService } from '@nestjs/jwt'; |
3 | 2 | import { |
4 | 3 | OnGatewayConnection, |
| 4 | + OnGatewayInit, |
5 | 5 | SubscribeMessage, |
6 | 6 | WebSocketGateway, |
7 | 7 | WebSocketServer, |
| 8 | + WsException, |
8 | 9 | } from '@nestjs/websockets'; |
| 10 | +import { ConfigService } from '@nestjs/config'; |
9 | 11 | import { Server, Socket } from 'socket.io'; |
10 | 12 | import { BoardTreesService } from './board-trees.service'; |
11 | | -import { |
12 | | - OperationAdd, |
13 | | - OperationDelete, |
14 | | - OperationMove, |
15 | | - OperationUpdate, |
16 | | -} from '../crdt/operation'; |
| 13 | +import type { BoardOperation } from './schemas/board-operation.schema'; |
17 | 14 |
|
18 | 15 | @WebSocketGateway({ namespace: 'board' }) |
19 | | -export class BoardTreesGateway implements OnGatewayConnection { |
| 16 | +export class BoardTreesGateway implements OnGatewayInit, OnGatewayConnection { |
20 | 17 | constructor( |
21 | 18 | private boardTreesService: BoardTreesService, |
22 | 19 | private jwtService: JwtService, |
| 20 | + private configService: ConfigService, |
23 | 21 | ) {} |
24 | 22 |
|
25 | 23 | @WebSocketServer() |
26 | 24 | server: Server; |
27 | 25 |
|
28 | | - handleConnection(client: Socket, token: string) { |
29 | | - if (!token) { |
30 | | - client.disconnect(); |
31 | | - throw new UnauthorizedException(); |
32 | | - } |
33 | | - try { |
34 | | - this.jwtService.verify(token); |
35 | | - } catch (error) { |
36 | | - client.disconnect(); |
37 | | - throw new UnauthorizedException(); |
38 | | - } |
| 26 | + afterInit(server: Server) { |
| 27 | + server.use((socket, next) => { |
| 28 | + const token = socket.handshake.auth.token; |
| 29 | + if (!token) { |
| 30 | + next(new WsException('access token required')); |
| 31 | + } |
| 32 | + try { |
| 33 | + this.jwtService.verify(token, { |
| 34 | + secret: this.configService.get<string>('JWT_ACCESS_SECRET'), |
| 35 | + }); |
| 36 | + next(); |
| 37 | + } catch (error) { |
| 38 | + next(new WsException('token is invalid')); |
| 39 | + } |
| 40 | + }); |
39 | 41 | } |
40 | 42 |
|
41 | | - @SubscribeMessage('joinBoard') |
42 | | - async handleJoinBoard(client: Socket, payload: string) { |
43 | | - const payloadObject = JSON.parse(payload); |
44 | | - if (!this.boardTreesService.hasTree(payloadObject.boardId)) { |
45 | | - await this.boardTreesService.initBoardTree( |
46 | | - payloadObject.boardId, |
47 | | - payloadObject.boardName, |
48 | | - ); |
| 43 | + handleConnection(client: Socket) { |
| 44 | + const query = client.handshake.query; |
| 45 | + const boardId = query.boardId; |
| 46 | + |
| 47 | + if (!boardId) { |
| 48 | + client.emit('board_id_required', new WsException('board id required')); |
| 49 | + client.disconnect(); |
49 | 50 | } |
50 | | - client.join(payloadObject.boardId); |
51 | | - client.emit( |
52 | | - 'initTree', |
53 | | - this.boardTreesService.getTreeData(payloadObject.boardId), |
54 | | - ); |
| 51 | + client.join(boardId); |
| 52 | + client.emit('board_joined', boardId); |
55 | 53 | } |
56 | 54 |
|
57 | | - @SubscribeMessage('updateMindmap') |
58 | | - handleUpdateMindmap(client: Socket, payload: string) { |
59 | | - const payloadObject = JSON.parse(payload); |
60 | | - const { boardId, operation: serializedOperation } = payloadObject; |
61 | | - |
62 | | - const operationTypeMap = { |
63 | | - add: OperationAdd.parse, |
64 | | - delete: OperationDelete.parse, |
65 | | - move: OperationMove.parse, |
66 | | - update: OperationUpdate.parse, |
67 | | - }; |
68 | | - |
69 | | - const operation = |
70 | | - operationTypeMap[serializedOperation.operationType](serializedOperation); |
71 | | - this.boardTreesService.applyOperation(boardId, operation); |
72 | | - this.boardTreesService.updateTreeData(boardId); |
| 55 | + @SubscribeMessage('createOperation') |
| 56 | + async handleCreateOperation(client: Socket, operation: BoardOperation) { |
| 57 | + await this.boardTreesService.createOperationLog(operation); |
| 58 | + client.broadcast.to(operation.boardId).emit('operation', operation); |
| 59 | + client.emit('operationCreated'); |
| 60 | + } |
73 | 61 |
|
74 | | - client.broadcast |
75 | | - .to(boardId) |
76 | | - .emit('operationFromServer', serializedOperation); |
| 62 | + @SubscribeMessage('getOperations') |
| 63 | + async handleGetOperations(client: Socket, boardId: string) { |
| 64 | + const operations = await this.boardTreesService.getOperationLogs(boardId); |
| 65 | + client.emit('getOperations', operations); |
77 | 66 | } |
78 | 67 | } |
0 commit comments