import { priorityCalculator } from '@models/middleware/trafficControl' ; import { urlParameters } from '@models/url' ; import { ConnectionInfo } from '@stores/ConnectionInfo' ; import errorInfo from '@stores/ErrorInfo' ; import participants from '@stores/participants/Participants' ; import contents from '@stores/sharedContents/SharedContents' ; import { EventEmitter } from 'events' ; import jquery from 'jquery' ; import JitsiMeetJS from 'lib-jitsi-meet' ; import { Store } from '../../stores/utils' ; import { Conference } from './Conference' ; import { ConnectionStates , ConnectionStatesType } from './Constants' ; declare const config ; export const JITSILOGLEVEL = 'warn' ; export const TRACKLOG = false ; export const CONNECTIONLOG = false ; export const trackLog = TRACKLOG ? console . log : ( a ) => { } ; export const connLog = CONNECTIONLOG ? console . log : ( a ) => { } ; export const connDebug = CONNECTIONLOG ? console . debug : ( a ) => { } ; declare const global ; global . $ = jquery ; global . jQuery = jquery ; export const initOptions = { useIPv6 : false , disableSimulcast : true , enableWindowOnErrorHandler : true , enableAnalyticsLogging : false , disableThiredPartyRequests : false , disableAudioLevels : false , desktopSharingChromeExtId : 'mbocklcggfhnbahlnepmldehdhpjfcjp' , desktopSharingChromeDisabled : false , desktopSharingChromeSources : [ 'screen' , 'window' ] , desktopSharingChromeMinExtVersion : '0.1' , desktopSharingFirefoxDisabled : false , desktopSharingFrameRate : { min : 0.3 , max : 30 } } ; export class Connection extends EventEmitter { private _jitsiConnection ? : JitsiMeetJS . JitsiConnection ; private _store : Store < ConnectionInfo > | undefined ; public conference = new Conference ( ) ; public version = '0.0.1' ; public state = ConnectionStates . DISCONNECTED ; public set Store ( store ) { this . _store = store ; } public init ( ) { Object . assign ( initOptions , config . rtc . screenOptions ) ; return new Promise < string > ( ( resolve , reject ) => { JitsiMeetJS . init ( initOptions ) ; JitsiMeetJS . setLogLevel ( JITSILOGLEVEL ) ; this . _jitsiConnection = new JitsiMeetJS . JitsiConnection ( null , undefined , config ) ; this . _jitsiConnection . addEventListener ( JitsiMeetJS . events . connection . CONNECTION_ESTABLISHED , ( ) => { this . onStateChanged ( ConnectionStates . CONNECTED ) ; resolve ( JitsiMeetJS . events . connection . CONNECTION_ESTABLISHED ) ; } ) ; this . _jitsiConnection . addEventListener ( JitsiMeetJS . events . connection . CONNECTION_FAILED , ( ) => { this . onStateChanged ( ConnectionStates . DISCONNECTED ) ; reject ( JitsiMeetJS . events . connection . CONNECTION_FAILED ) ; } ) ; this . _jitsiConnection . addEventListener ( JitsiMeetJS . events . connection . CONNECTION_DISCONNECTED , ( ) => { this . onStateChanged ( ConnectionStates . DISCONNECTED ) ; } ) ; this . _jitsiConnection . connect ( ) ; this . onStateChanged ( ConnectionStates . CONNECTING ) ; } ) ; } public joinConference ( conferenceName ) { if ( this . _jitsiConnection ) { this . conference . init ( conferenceName , ( ) => { return this . _jitsiConnection ?. initJitsiConference ( conferenceName . toLowerCase ( ) , config ) ; } ) ; return ; } throw new Error ( 'No connection has been established.' ) ; } public leaveConference ( ) { return this . conference . uninit ( ) ; } public disconnect ( ) { if ( this . _jitsiConnection ) { connLog ( 'Disconnection order has been sent.' ) ; return this . _jitsiConnection ?. disconnect ( ) ; } return Promise . reject ( 'No connection has been established.' ) ; } reconnect ( ) { errorInfo . setType ( 'retry' ) ; participants . leaveAll ( ) ; contents . clearAllRemotes ( ) ; priorityCalculator . clear ( ) ; this . leaveConference ( ) . then ( ( ) => { console . log ( 'Disconnected but succeed in leaving... strange ... try to join again.' ) ; } ) . catch ( ( ) => { console . log ( 'Disconnected and failed to leave... try to join again' ) ; this . conference . bmRelaySocket ?. close ( ) ; } ) . finally ( ( ) => { if ( urlParameters . testBot !== null ) { window . location . reload ( ) ; } } ) ; } private onStateChanged ( state ) { if ( this . state !== state && state === ConnectionStates . DISCONNECTED ) { setTimeout ( ( ) => { this . _jitsiConnection ! . disconnect ( ) . finally ( ( ) => { setTimeout ( this . reconnect . bind ( this ) , 1000 ) ; } ) ; } , 1000 ) ; } this . state = state ; if ( this . _store ) { this . _store . changeState ( this . state ) ; } } }