import { DriverInterface } from '../types/driver' ; import { OptionsInterface } from '../types/options' ; import { ChannelInterface } from '../types/channel' ; import { RealTimeLog } from '../logger' ; module author Jonathan Casarrubias <t:@johncasarrubias, gh:github.com/mean-expert-official>
 *  license description export class WebRTCSignaler { static driver : DriverInterface ; static options : OptionsInterface ; static channels : ChannelInterface = { } ; constructor ( driver , options ) { RealTimeLog . log ( `WebRTCSignaler server enabled using ${ options . driver . name } driver.` ) ; WebRTCSignaler . driver = driver ; WebRTCSignaler . options = options ; WebRTCSignaler . driver . onConnection ( ( socket ) => { let initiatorChannel = '' ; socket . on ( 'new-channel' , ( data ) => { if ( ! WebRTCSignaler . channels [ data . channel ] ) { initiatorChannel = data . channel ; } WebRTCSignaler . channels [ data . channel ] = data . channel ; WebRTCSignaler . onNewNamespace ( data . channel , data . sender ) ; } ) ; socket . on ( 'presence' , ( channel ) => { var isChannelPresent = ! ! WebRTCSignaler . channels [ channel ] ; socket . emit ( 'presence' , isChannelPresent ) ; } ) ; socket . on ( 'disconnect' , ( channel ) => { if ( initiatorChannel ) { delete WebRTCSignaler . channels [ initiatorChannel ] ; } } ) ; } ) ; return WebRTCSignaler ; } static onNewNamespace ( channel , sender ) { WebRTCSignaler . driver . of ( '/' + channel ) . on ( 'connection' , ( socket ) => { let username ; if ( WebRTCSignaler . driver . isConnected ) { WebRTCSignaler . driver . isConnected = false ; socket . emit ( 'connect' , true ) ; } socket . on ( 'message' , ( data ) => { if ( data . sender == sender ) { if ( ! username ) username = data . data . sender ; socket . broadcast . emit ( 'message' , data . data ) ; } } ) ; socket . on ( 'disconnect' , ( ) => { if ( username ) { socket . broadcast . emit ( 'user-left' , username ) ; username = null ; } } ) ; } ) ; } }