import { Log } from './../log' ; export class Channel { type { string [ ] } protected _clientEventPatterns : string [ ] = [ 'client-*' , ] ; type { string [ ] } protected static _privateChannelPatterns : string [ ] = [ 'private-*' , 'presence-*' , ] ; param { any } io param { any } stats param { any } options constructor ( protected io , protected stats , protected options ) { } param { any } socket param { any } data return { Promise < any > } join ( socket , data ) { return new Promise ( ( resolve , reject ) => { socket . join ( data . channel ) ; this . onJoin ( socket , data . channel , null ) ; resolve ( { socket , data } ) ; } ) ; } param { any } socket param { string } channel param { string } reason return { void } leave ( socket , channel , reason ) { if ( channel ) { socket . leave ( channel ) ; if ( this . options . development ) { Log . info ( { time : new Date ( ) . toISOString ( ) , socketId : socket . id , action : 'leave' , channel , reason , } ) ; } } } param { any } socket param { string } channel param { any } member return { void } onJoin ( socket , channel , member ) { socket . emit ( 'channel:joined' , channel ) ; if ( this . options . development ) { Log . info ( { time : new Date ( ) . toISOString ( ) , socketId : socket . id , action : 'onJoin' , member , channel , } ) ; } } param { any } socket param { any } data return { void } onClientEvent ( socket , data ) { try { data = JSON . parse ( data ) ; } catch ( e ) { } if ( this . options . development ) { Log . info ( { time : new Date ( ) . toISOString ( ) , socketId : socket . id , action : 'client event' , status : 'received' , data , } ) ; } if ( data . event && data . channel ) { if ( this . isClientEvent ( data . event ) && this . isInChannel ( socket , data . channel ) ) { this . io . of ( this . getNspForSocket ( socket ) ) . sockets . get ( socket . id ) . broadcast . to ( data . channel ) . emit ( data . event , data . channel , data . data ) ; this . stats . markWsMessage ( socket . echoApp ) ; if ( this . options . development ) { Log . info ( { time : new Date ( ) . toISOString ( ) , socketId : socket . id , action : 'client event' , status : 'success' , data , } ) ; } } } } param { string } event return { boolean } isClientEvent ( event ) { let isClientEvent = false ; this . _clientEventPatterns . forEach ( pattern => { let regex = new RegExp ( pattern . replace ( '*' , '.*' ) ) ; if ( regex . test ( event ) ) { isClientEvent = true ; } } ) ; return isClientEvent ; } param { any } socket param { string } channel return { boolean } isInChannel ( socket , channel ) { return socket . adapter . rooms . has ( channel ) ; } param { any } socket param { string } channel return { boolean } static isInChannel ( socket , channel ) { return this . isInChannel ( socket , channel ) ; } param { any } socket return getNspForSocket ( socket ) { return socket ? socket . nsp . name : '/' ; } param { any } socket return static getNspForSocket ( socket ) { return this . getNspForSocket ( socket ) ; } param { string } channel return { boolean } static isPrivateChannel ( channel ) { let isPrivate = false ; this . _privateChannelPatterns . forEach ( pattern => { let regex = new RegExp ( pattern . replace ( '*' , '.*' ) ) ; if ( regex . test ( channel ) ) { isPrivate = true ; } } ) ; return isPrivate ; } param { string } channel return { boolean } static isPresenceChannel ( channel ) { return channel . lastIndexOf ( 'presence-' , 0 ) === 0 ; } }