@@ -440,36 +440,90 @@ export class SocketGateway
440440 }
441441
442442 /**
443- * Removes a client from the queues if they are in any.
443+ * Event called when a client leaves the priority queue
444444 *
445+ * @SubscribeMessage 'leavePriorityQueue'
445446 * @param {Socket } client - The client socket
446- * @returns {void }
447+ * @returns {Promise< void> }
447448 */
448- private removeFromQueue ( client : Socket ) : void {
449- // Remove the client from the shared queue with the specified client ID
449+ @SubscribeMessage ( 'leavePriorityQueue' )
450+ public async leavePriorityQueue (
451+ @ConnectedSocket ( ) client : Socket ,
452+ ) : Promise < void > {
453+ // Get the user ID from the client
454+ const userId : string = this . socketService . getUserId ( client . id ) ;
455+ if ( ! userId ) return ;
456+
457+ // Remove the client from the priority queue
458+ this . priorityQueue = new Queue < QueueMember > (
459+ this . priorityQueue
460+ . getItems ( )
461+ . filter ( ( queueItem : QueueMember ) => queueItem . userId !== userId ) ,
462+ ) ;
463+
464+ // Notify the clients with the updated queue size
465+ this . server . emit ( 'priorityQueue' , {
466+ size : this . priorityQueue . size ( ) ,
467+ } ) ;
468+
469+ // Get the user's socket IDs
470+ const userSocketIds : string [ ] = this . socketService . getUserSocketIds ( userId ) ;
471+ if ( ! userSocketIds . length ) return ;
472+
473+ // Notify the client that they have left the queue
474+ userSocketIds . forEach ( ( socketId : string ) => {
475+ this . server . to ( socketId ) . emit ( 'priorityQueueStatus' , {
476+ status : 'left' ,
477+ userId,
478+ } ) ;
479+ } ) ;
480+
481+ console . log (
482+ `Client removed from priority queue. Shared queue size: ${ this . sharedQueue . size ( ) } , Priority queue size: ${ this . priorityQueue . size ( ) } .` ,
483+ ) ;
484+ }
485+
486+ /**
487+ * Event called when a client leaves the shared queue
488+ *
489+ * @SubscribeMessage 'leaveSharedQueue'
490+ * @param {Socket } client - The client socket
491+ * @returns {Promise<void> }
492+ */
493+ @SubscribeMessage ( 'leaveSharedQueue' )
494+ public async leaveSharedQueue (
495+ @ConnectedSocket ( ) client : Socket ,
496+ ) : Promise < void > {
497+ // Get the user ID from the client
498+ const userId : string = this . socketService . getUserId ( client . id ) ;
499+ if ( ! userId ) return ;
500+
501+ // Remove the client from the shared queue
450502 this . sharedQueue = new Queue < QueueMember > (
451503 this . sharedQueue
452504 . getItems ( )
453- . filter ( ( queueItem : QueueMember ) => queueItem . client . id !== client . id ) ,
505+ . filter ( ( queueItem : QueueMember ) => queueItem . userId !== userId ) ,
454506 ) ;
455507
508+ // Notify the clients with the updated queue size
456509 this . server . emit ( 'sharedQueue' , {
457510 size : this . sharedQueue . size ( ) ,
458511 } ) ;
459512
460- // Remove the client from the priority queue with the specified client ID
461- this . priorityQueue = new Queue < QueueMember > (
462- this . priorityQueue
463- . getItems ( )
464- . filter ( ( queueItem : QueueMember ) => queueItem . client . id !== client . id ) ,
465- ) ;
513+ // Get the user's socket IDs
514+ const userSocketIds : string [ ] = this . socketService . getUserSocketIds ( userId ) ;
515+ if ( ! userSocketIds . length ) return ;
466516
467- this . server . emit ( 'priorityQueue' , {
468- size : this . priorityQueue . size ( ) ,
517+ // Notify the client that they have left the queue
518+ userSocketIds . forEach ( ( socketId : string ) => {
519+ this . server . to ( socketId ) . emit ( 'sharedQueueStatus' , {
520+ status : 'left' ,
521+ userId,
522+ } ) ;
469523 } ) ;
470524
471525 console . log (
472- `Client removed from queue. Shared queue size: ${ this . sharedQueue . size ( ) } , Priority queue size: ${ this . priorityQueue . size ( ) } .` ,
526+ `Client removed from shared queue. Shared queue size: ${ this . sharedQueue . size ( ) } , Priority queue size: ${ this . priorityQueue . size ( ) } .` ,
473527 ) ;
474528 }
475529
0 commit comments