@@ -6,30 +6,35 @@ import {
6
6
OnGatewayConnection ,
7
7
OnGatewayDisconnect ,
8
8
MessageBody ,
9
- ConnectedSocket ,
9
+ ConnectedSocket
10
10
} from '@nestjs/websockets' ;
11
11
import { UseGuards } from '@nestjs/common' ;
12
12
import { Server , Socket } from 'socket.io' ;
13
13
import {
14
- CHATTING_SOCKET_DEFAULT_EVENT ,
15
- CHATTING_SOCKET_RECEIVE_EVENT , CHATTING_SOCKET_SEND_EVENT
14
+ CHATTING_SOCKET_DEFAULT_EVENT , CHATTING_SOCKET_RECEIVE_EVENT , CHATTING_SOCKET_SEND_EVENT
16
15
} from '../event/constants' ;
17
16
import {
17
+ BanUserIncomingMessageDto ,
18
18
NormalIncomingMessageDto , NoticeIncomingMessageDto , QuestionDoneIncomingMessageDto , QuestionIncomingMessageDto
19
19
} from '../event/dto/IncomingMessage.dto' ;
20
20
import { JoiningRoomDto } from '../event/dto/JoiningRoom.dto' ;
21
21
import { RoomService } from '../room/room.service' ;
22
22
import { createAdapter } from '@socket.io/redis-adapter' ;
23
- import { HostGuard , MessageGuard } from './chat.guard' ;
23
+ import { BlacklistGuard , HostGuard , MessageGuard } from './chat.guard' ;
24
24
import { LeavingRoomDto } from '../event/dto/LeavingRoom.dto' ;
25
25
import {
26
26
NormalOutgoingMessageDto ,
27
27
NoticeOutgoingMessageDto ,
28
28
QuestionOutgoingMessageDto
29
29
} from '../event/dto/OutgoingMessage.dto' ;
30
30
import { QuestionDto } from '../event/dto/Question.dto' ;
31
+ import { ChatException , CHATTING_SOCKET_ERROR } from './chat.error' ;
31
32
32
- @WebSocketGateway ( { cors : true } )
33
+ @WebSocketGateway ( {
34
+ cors : true ,
35
+ pingInterval : 30000 ,
36
+ pingTimeout : 10000 ,
37
+ } )
33
38
export class ChatGateway implements OnGatewayInit , OnGatewayConnection , OnGatewayDisconnect {
34
39
constructor ( private roomService : RoomService ) { } ;
35
40
@@ -46,7 +51,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
46
51
47
52
async handleConnection ( client : Socket ) {
48
53
console . log ( `Client connected: ${ client . id } ` ) ;
49
- const user = await this . roomService . createUser ( client . id ) ;
54
+ const user = await this . roomService . createUser ( client ) ;
50
55
console . log ( user ) ;
51
56
52
57
/*
@@ -69,6 +74,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
69
74
}
70
75
71
76
// 특정 방에 참여하기 위한 메서드
77
+ @UseGuards ( BlacklistGuard )
72
78
@SubscribeMessage ( CHATTING_SOCKET_DEFAULT_EVENT . JOIN_ROOM )
73
79
async handleJoinRoom ( client : Socket , payload : JoiningRoomDto ) {
74
80
const { roomId, userId } = payload ;
@@ -93,17 +99,20 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
93
99
}
94
100
95
101
// 방에 NORMAL 메시지를 보내기 위한 메서드
96
- @UseGuards ( MessageGuard )
102
+ @UseGuards ( MessageGuard , BlacklistGuard )
97
103
@SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . NORMAL )
98
104
async handleNormalMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : NormalIncomingMessageDto ) {
99
105
const { roomId, userId, msg } = payload ;
100
106
const user = await this . roomService . getUserByClientId ( client . id ) ;
101
107
const normalOutgoingMessage : Omit < NormalOutgoingMessageDto , 'owner' > = {
102
108
roomId,
103
- ...user ,
109
+ nickname : user . nickname ,
110
+ color : user . color ,
111
+ entryTime : user . entryTime ,
104
112
msg,
105
113
msgTime : new Date ( ) . toISOString ( ) ,
106
- msgType : 'normal'
114
+ msgType : 'normal' ,
115
+ socketId : client . id
107
116
} ;
108
117
console . log ( 'Normal Message Come In: ' , normalOutgoingMessage ) ;
109
118
const hostId = await this . roomService . getHostOfRoom ( roomId ) ;
@@ -121,18 +130,21 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
121
130
}
122
131
123
132
// 방에 QUESTION 메시지를 보내기 위한 메서드
124
- @UseGuards ( MessageGuard )
133
+ @UseGuards ( MessageGuard , BlacklistGuard )
125
134
@SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . QUESTION )
126
135
async handleQuestionMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : QuestionIncomingMessageDto ) {
127
136
const { roomId, msg } = payload ;
128
137
const user = await this . roomService . getUserByClientId ( client . id ) ;
129
138
const questionWithoutId : Omit < QuestionDto , 'questionId' > = {
130
139
roomId,
131
- ...user ,
140
+ nickname : user . nickname ,
141
+ color : user . color ,
142
+ entryTime : user . entryTime ,
132
143
msg,
133
144
msgTime : new Date ( ) . toISOString ( ) ,
134
145
msgType : 'question' ,
135
- questionDone : false
146
+ questionDone : false ,
147
+ socketId : client . id
136
148
} ;
137
149
138
150
const question : QuestionOutgoingMessageDto = await this . roomService . addQuestion ( roomId , questionWithoutId ) ;
@@ -150,19 +162,34 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
150
162
}
151
163
152
164
// 방에 NOTICE 메시지를 보내기 위한 메서드
153
- @UseGuards ( MessageGuard )
154
- @UseGuards ( HostGuard )
165
+ @UseGuards ( MessageGuard , HostGuard )
155
166
@SubscribeMessage ( CHATTING_SOCKET_SEND_EVENT . NOTICE )
156
167
async handleNoticeMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : NoticeIncomingMessageDto ) {
157
168
const { roomId, msg } = payload ;
158
169
const user = await this . roomService . getUserByClientId ( client . id ) ;
159
170
const noticeOutgoingMessage : NoticeOutgoingMessageDto = {
160
171
roomId,
161
- ...user ,
172
+ nickname : user . nickname ,
173
+ color : user . color ,
174
+ entryTime : user . entryTime ,
162
175
msg,
163
176
msgTime : new Date ( ) . toISOString ( ) ,
164
177
msgType : 'notice'
165
178
} ;
166
179
this . server . to ( roomId ) . emit ( CHATTING_SOCKET_RECEIVE_EVENT . NOTICE , noticeOutgoingMessage ) ;
167
180
}
181
+
182
+ @UseGuards ( HostGuard )
183
+ @SubscribeMessage ( CHATTING_SOCKET_DEFAULT_EVENT . BAN_USER )
184
+ async handleBanUserMessage ( @ConnectedSocket ( ) client : Socket , @MessageBody ( ) payload : BanUserIncomingMessageDto ) {
185
+ const { roomId, socketId } = payload ;
186
+ const banUser = await this . roomService . getUserByClientId ( socketId ) ;
187
+ console . log ( 'banUSer = ' , banUser ) ;
188
+ if ( ! banUser ) throw new ChatException ( CHATTING_SOCKET_ERROR . INVALID_USER , roomId ) ;
189
+ const { address, userAgent } = banUser ;
190
+ if ( ! userAgent ) throw new ChatException ( CHATTING_SOCKET_ERROR . INVALID_USER , roomId ) ;
191
+
192
+ await this . roomService . addUserToBlacklist ( roomId , address , userAgent ) ;
193
+ console . log ( await this . roomService . getUserBlacklist ( roomId , address ) ) ;
194
+ }
168
195
}
0 commit comments