@@ -15,6 +15,7 @@ import {ErrorHandler} from "../ErrorAndLog/ErrorHandler";
15
15
import { RoomManager } from "../GameLogic/RoomManager" ;
16
16
import { UserNotExistError , WrongPasswordError } from "../ErrorAndLog/GError" ;
17
17
import { Player } from "../GameLogic/Player"
18
+ import { SessionManager } from "../Communication/SessionManager" ;
18
19
19
20
/**
20
21
* GServices 类,用于管理服务器的各种服务和功能。
@@ -29,6 +30,7 @@ export class GServices {
29
30
private httpServer ! : HTTPServer ;
30
31
private webSocketServer ! : WebSocketServer ;
31
32
private authentication ! : Authentication ;
33
+ private sessionManager ! : SessionManager ;
32
34
33
35
private static _services : GServices ;
34
36
public static I ( ) {
@@ -71,6 +73,7 @@ export class GServices {
71
73
process . on ( 'SIGINT' , this . shutdown . bind ( this ) ) ;
72
74
process . on ( 'SIGTERM' , this . shutdown . bind ( this ) ) ;
73
75
76
+ this . sessionManager = new SessionManager ( ) ;
74
77
this . authentication = new Authentication ( config ) ;
75
78
await this . authentication . DataBase . createConnection ( ) ;
76
79
@@ -125,6 +128,9 @@ export class GServices {
125
128
* @param connection - 连接对象。
126
129
*/
127
130
public handleClientConnect ( connection : Connection ) : void {
131
+ const sessionId = this . sessionManager . createSession ( connection ) ;
132
+ // 发送sessionId给客户端
133
+ WebSocketUtils . sendToConnection ( connection , { type : 'sessionId' , payload : sessionId } ) ;
128
134
// 调用用户自定义的连接建立处理方法
129
135
this . invokeExtensionMethod ( 'onClientConnect' , connection ) ;
130
136
}
@@ -134,6 +140,9 @@ export class GServices {
134
140
* @param connection - 连接对象。
135
141
*/
136
142
public handleClientDisconnect ( connection : Connection ) : void {
143
+ // 当客户端断开连接时,我们不立即删除其session
144
+ // 可以设置一个定时器,在一段时间后删除session
145
+ setTimeout ( ( ) => this . sessionManager . deleteSession ( connection . sessionId ) , this . config . sessionExpireTime ) ;
137
146
// 调用用户自定义的连接断开处理方法
138
147
this . invokeExtensionMethod ( 'onClientDisconnect' , connection ) ;
139
148
}
@@ -176,6 +185,12 @@ export class GServices {
176
185
*/
177
186
public async handleMessage ( connection : Connection , message : Message ) {
178
187
try {
188
+ // 对重连请求进行特殊处理
189
+ if ( message . type === 'reconnect' ) {
190
+ this . handleReconnect ( connection , message . payload ) ;
191
+ return ;
192
+ }
193
+
179
194
if ( ! connection . isAuthenticated ) {
180
195
// 如果连接还没有经过身份验证,那么它只能发送身份验证消息
181
196
if ( message . type !== 'authentication' ) {
@@ -199,6 +214,10 @@ export class GServices {
199
214
} else {
200
215
// 身份验证通过
201
216
connection . isAuthenticated = true ;
217
+ // 获取sessionId
218
+ const sessionId = connection . sessionId ;
219
+ // 发送身份验证消息,带有sessionId
220
+ WebSocketUtils . sendToConnection ( connection , { type : 'authentication' , payload : { sessionId : sessionId } } ) ;
202
221
return ;
203
222
}
204
223
@@ -214,10 +233,6 @@ export class GServices {
214
233
// 处理数据消息
215
234
this . handleDataMessage ( connection , message . payload ) ;
216
235
break ;
217
- case 'authentication' :
218
- // 处理身份验证消息
219
- this . authentication . handleAuthenticationMessage ( connection , message ) ;
220
- break ;
221
236
case 'stateUpdate' :
222
237
this . handleStateUpdate ( connection , message ) ;
223
238
break ;
@@ -250,6 +265,24 @@ export class GServices {
250
265
this . invokeExtensionMethod ( 'onMessageReceived' , connection , message ) ;
251
266
}
252
267
268
+ private handleReconnect ( connection : Connection , payload : any ) : void {
269
+ const { sessionId } = payload ;
270
+ const oldConnection = this . sessionManager . getSession ( sessionId ) ;
271
+ if ( oldConnection ) {
272
+ // 重新绑定sessionId到新连接
273
+ connection . sessionId = sessionId ;
274
+ this . sessionManager . createSession ( connection ) ;
275
+ // 恢复状态
276
+ connection . state = oldConnection . state ;
277
+ connection . roomId = oldConnection . roomId ;
278
+ // 发送重连成功消息
279
+ WebSocketUtils . sendToConnection ( connection , { type : 'reconnectSuccess' , payload : null } ) ;
280
+ } else {
281
+ // sessionId无效,发送重连失败消息
282
+ WebSocketUtils . sendToConnection ( connection , { type : 'reconnectFail' , payload : null } ) ;
283
+ }
284
+ }
285
+
253
286
/**
254
287
* 处理状态更新消息。
255
288
* @param connection - 连接对象。
0 commit comments