@@ -72,17 +72,20 @@ export default class Client extends SendStuff implements IClient {
7272 // used internally in packet.ts
7373 /** @type {Buffer } */
7474 halfpack : Buffer ;
75-
7675 /** @type {any[] } */
7776 packetQueue : any [ ] ;
7877
78+ // used internally in server.ts
79+ bindTCP ;
80+ bindWS ;
81+
7982 /** @type {PlayerEntity } */
8083 entity : PlayerEntity = null ;
8184
8285 /** @type {number } */
8386 ping : number ;
8487
85- room_join_timer : number = - 1 ; // if >0 - joined a room recently
88+ room_join_timer : number = - 1 ; // if >0 - joined a room recently, getting FULL entities list
8689 reconnect_timer : number = - 1 ;
8790
8891 /** @type {boolean } */
@@ -231,21 +234,14 @@ export default class Client extends SendStuff implements IClient {
231234 }
232235 }
233236
234- disconnect ( ) {
235- if ( this . socket_type === 'ws' ) {
236- ( this . socket as WebSocket ) . close ( ) ;
237- }
238- else {
239- ( this . socket as TCPSocket ) . destroy ( ) ;
240- }
241- }
242-
243237 onDisconnect ( ) {
244238 this . socket = null ;
245239 this . connected = false ;
246240
247241 this . reconnect_timer = config . reconnect_timeout ;
248242
243+ this . matchMakingStop ( ) ;
244+
249245 // go offline
250246 if ( this . logged_in ) {
251247 this . profile . online = false ;
@@ -254,6 +250,29 @@ export default class Client extends SendStuff implements IClient {
254250 }
255251 }
256252
253+ onReconnect ( ) {
254+ if ( this . lobby )
255+ this . sendLobbyJoin ( this . lobby ) ;
256+ if ( this . room && this . entity )
257+ this . sendPlay ( this . lobby , this . room , this . entity . pos , this . entity . uuid ) ;
258+
259+ this . room_join_timer = global . config . room . recently_joined_timer ;
260+ }
261+
262+
263+ disconnect ( ) {
264+ if ( this . socket === null ) {
265+ return ;
266+ }
267+
268+ if ( this . socket_type === 'ws' ) {
269+ ( this . socket as WebSocket ) . close ( ) ;
270+ }
271+ else {
272+ ( this . socket as TCPSocket ) . destroy ( ) ;
273+ }
274+ }
275+
257276 destroy ( ) {
258277 // save everything to the DB
259278 this . save ( ) ;
@@ -267,31 +286,38 @@ export default class Client extends SendStuff implements IClient {
267286 this . party . kickMember ( this , 'disconnect' , true ) ;
268287
269288
270- global . clients . splice ( global . clients . indexOf ( this ) , 1 ) ;
289+ let idx = global . clients . indexOf ( this ) ;
290+ if ( idx != - 1 )
291+ global . clients . splice ( idx , 1 ) ;
271292
272293 this . disconnect ( ) ;
273294 }
274295
275- // insert this socket into a "dead" (disconnected) client
276- replace ( old_client : Client ) {
277- if ( old_client . connected ) {
278- return null ;
296+ // move a new client's socket into this "dead" (disconnected) client
297+ // removes the other client from the global.clients list
298+ reconnect ( new_client : Client ) {
299+ if ( this . connected ) {
300+ return ;
279301 }
280302
281- old_client . socket = this . socket ;
282- old_client . socket_type = this . socket_type ;
303+ this . connected = true ;
283304
284- // delete self from the list
285- global . clients . splice ( global . clients . indexOf ( this ) , 1 ) ;
305+ this . socket = new_client . socket ;
306+ this . socket_type = new_client . socket_type ;
286307
287- return old_client ;
288- }
308+ this . socket . removeAllListeners ( ) ;
309+
310+ if ( this . socket_type === 'tcp' )
311+ this . bindTCP ( this . socket ) ;
312+ else
313+ this . bindWS ( this . socket ) ;
289314
290- onReconnect ( ) {
291- if ( this . lobby )
292- this . sendLobbyJoin ( this . lobby ) ;
293- if ( this . room && this . entity )
294- this . sendPlay ( this . lobby , this . room , this . entity . pos , this . entity . uuid ) ;
315+ // delete self from the list
316+ let idx = global . clients . indexOf ( new_client ) ;
317+ if ( idx != - 1 )
318+ global . clients . splice ( idx , 1 ) ;
319+
320+ this . onReconnect ( ) ;
295321 }
296322
297323
@@ -517,7 +543,7 @@ export default class Client extends SendStuff implements IClient {
517543 }
518544
519545 matchMakingStart ( req :MatchRequirements ) :Ticket | string {
520- if ( this . ticket ) return 'already matchmaking' ;
546+ if ( this . ticket !== null ) return 'already matchmaking' ;
521547 if ( this . match ) return 'already in a match' ;
522548
523549 if ( this . party ) {
@@ -554,9 +580,9 @@ export default class Client extends SendStuff implements IClient {
554580 /**
555581 * Save account and profile data to the DB
556582 */
557- save ( ) {
583+ async save ( ) {
558584 if ( this . account !== null ) {
559- this . account . save ( )
585+ await this . account . save ( )
560586 . then ( ( ) => {
561587 // trace('Saved the account successfully');
562588 } )
@@ -570,7 +596,7 @@ export default class Client extends SendStuff implements IClient {
570596 this . profile . state . lobbyid = this . lobby . lobbyid ;
571597 }
572598
573- this . profile . save ( )
599+ await this . profile . save ( )
574600 . then ( ( ) => {
575601 // trace('Saved the profile successfully.');
576602 } )
@@ -579,7 +605,7 @@ export default class Client extends SendStuff implements IClient {
579605 } ) ;
580606 }
581607 if ( this . session !== null ) {
582- this . session . save ( )
608+ await this . session . save ( )
583609 . then ( ( ) => {
584610 // trace('Saved the session successfully');
585611 } )
0 commit comments