@@ -84,16 +84,16 @@ class SocketServer extends EventEmitter {
8484
8585 self . emit ( 'read.object' , data ) ;
8686
87- if ( self . authenticate && options . token ) {
87+ if ( self . authenticate ) {
8888 const { user_id, expires } = self . authenticate . decodeToken ( options . token )
8989
9090 if ( user_id ) {
9191 socket . user_id = user_id ;
9292 socket . expires = expires ;
9393 self . emit ( 'userStatus' , { socket, method : 'userStatus' , user_id, userStatus : 'on' , organization_id } ) ;
94- this . emit ( "notification.user" , socket )
94+ self . emit ( "notification.user" , socket )
9595 } else
96- self . emit ( 'userStatus' , { socket, user_id, status : 'off' , organization_id } ) ;
96+ self . emit ( 'userStatus' , { socket, user_id : options . user_id , userStatus : 'off' , organization_id } ) ;
9797
9898 self . onWebSocket ( socket ) ;
9999
@@ -173,12 +173,13 @@ class SocketServer extends EventEmitter {
173173
174174 if ( socket . user_id ) {
175175 this . emit ( 'userStatus' , { socket, user_id : socket . user_id , userStatus : 'on' , organization_id } ) ;
176+ let user = this . users . get ( socket . user_id )
176177
177- if ( ! this . users . has ( socket . user_id ) ) {
178+ if ( ! Array . isArray ( user ) ) {
179+ clearTimeout ( user )
178180 this . users . set ( socket . user_id , [ socket ] )
179- } else {
180- this . users . get ( socket . user_id ) . push ( socket )
181- }
181+ } else
182+ user . push ( socket )
182183 }
183184
184185 }
@@ -277,13 +278,22 @@ class SocketServer extends EventEmitter {
277278 if ( socket . user_id ) {
278279 let sockets = this . users . get ( socket . user_id )
279280 if ( sockets ) {
280- const index = sockets . findIndex ( item => item . id === socket . id ) ;
281- if ( index !== - 1 ) {
282- sockets . splice ( index , 1 ) ;
283- }
284- if ( ! sockets . length ) {
285- this . users . delete ( socket . user_id ) ;
286- this . emit ( 'userStatus' , { socket, user_id, status : 'off' , organization_id } ) ;
281+ if ( Array . isArray ( sockets ) && sockets . length ) {
282+ const index = sockets . findIndex ( item => item . id === socket . id ) ;
283+ if ( index !== - 1 ) {
284+ sockets . splice ( index , 1 ) ;
285+ }
286+ } else {
287+ let userDebounceTimer = sockets
288+
289+ clearTimeout ( userDebounceTimer ) ;
290+ userDebounceTimer = setTimeout ( ( ) => {
291+ this . users . delete ( socket . user_id ) ;
292+ this . emit ( 'userStatus' , { socket, user_id : socket . user_id , userStatus : 'off' , organization_id } ) ;
293+ } , 10000 ) ;
294+
295+ this . users . set ( socket . user_id , userDebounceTimer )
296+
287297 }
288298 }
289299 }
0 commit comments