/* eslint-disable no-underscore-dangle */ /* eslint-disable valid-jsdoc */ /* eslint-disable @typescript-eslint/no-shadow */ import * as Media from '@webex/internal-media-core'; import {Mutex} from 'async-mutex'; import {METHOD_START_MESSAGE} from '../common/constants'; import { filterMobiusUris, handleCallingClientErrors, normalizeMobiusUris, isValidServiceDomain, uploadLogs, validateServiceData, } from '../common/Utils'; import {LOGGER} from '../Logger/types'; import SDKConnector from '../SDKConnector'; import {ClientRegionInfo, ISDKConnector, ServiceHost, WebexSDK} from '../SDKConnector/types'; import {Eventing} from '../Events/impl'; import { CallingClientEventTypes, MOBIUS_EVENT_KEYS, CallSessionEvent, SessionType, CALLING_CLIENT_EVENT_KEYS, } from '../Events/types'; import { ServiceIndicator, RegionInfo, ALLOWED_SERVICES, HTTP_METHODS, MobiusServers, ServiceData, WebexRequestPayload, RegistrationStatus, UploadLogsResponse, DeviceType, Devices, } from '../common/types'; import {ICallingClient, CallingClientConfig} from './types'; import {ICall, ICallManager, MobiusAsyncEvent, MobiusEventType} from './calling/types'; import log from '../Logger'; import {getCallManager} from './calling/callManager'; import { CALLING_CLIENT_FILE, CALLS_CLEARED_HANDLER_UTIL, CALLING_USER_AGENT, CISCO_DEVICE_URL, DISCOVERY_URL, GET_MOBIUS_SERVERS_UTIL, SPARK_USER_AGENT, URL_ENDPOINT, API_V1, METHODS, NETWORK_FLAP_TIMEOUT, DEVICES_ENDPOINT_RESOURCE, WCC_CALLING_RTMS_DOMAIN, } from './constants'; import Line from './line'; import {ILine} from './line/types'; import { METRIC_EVENT, REG_ACTION, METRIC_TYPE, IMetricManager, CONNECTION_ACTION, MOBIUS_SERVER_ACTION, MOBIUS_SOCKET_ACTION, } from '../Metrics/types'; import {getMetricManager} from '../Metrics'; import windowsChromiumIceWarmup from './windowsChromiumIceWarmupUtils'; import {APIRequest} from './utils/request'; /** * The `CallingClient` module provides a set of APIs for line registration and calling functionalities within the SDK. * * The following code snippet demonstrates how to create an instance of `CallingClient` using a `webex` instance and `callingConfig`: * * @example * ```javascript * const callingClient = createClient(webex, callingConfig); * ``` */ export class CallingClient extends Eventing implements ICallingClient { private sdkConnector: ISDKConnector; private webex: WebexSDK; private mutex: Mutex; private callManager: ICallManager; private metricManager: IMetricManager; private sdkConfig?: CallingClientConfig; private serviceData: ServiceData; private primaryMobiusUris: string[]; private backupMobiusUris: string[]; private primaryWssMobiusUris: string[]; private backupWssMobiusUris: string[]; private mobiusClusters: ServiceHost[]; private mobiusHost: string; public mediaEngine: typeof Media; private lineDict: Record = {}; private apiRequest: APIRequest; private isNetworkDown = false; private networkDownTimestamp = ''; private networkUpTimestamp = ''; private mercuryDownTimestamp = ''; private mercuryUpTimestamp = ''; /** * @ignore */ constructor(webex: WebexSDK, config?: CallingClientConfig) { super(); this.sdkConnector = SDKConnector; if (!this.sdkConnector.getWebex()) { SDKConnector.setWebex(webex); if (config?.logger?.level && webex.logger.config) { webex.logger.config.level = config.logger.level; // override the webex logger level } log.setWebexLogger(webex.logger); } this.mutex = new Mutex(); this.webex = this.sdkConnector.getWebex(); this.sdkConfig = config; this.serviceData = this.sdkConfig?.serviceData?.indicator ? this.sdkConfig.serviceData : {indicator: ServiceIndicator.CALLING, domain: ''}; const logLevel = this.sdkConfig?.logger?.level ? this.sdkConfig.logger.level : LOGGER.ERROR; log.setLogger(logLevel, CALLING_CLIENT_FILE); validateServiceData(this.serviceData); this.callManager = getCallManager( this.webex, this.serviceData.indicator, this.sdkConfig?.iceGathering ); this.metricManager = getMetricManager(this.webex, this.serviceData.indicator); this.mediaEngine = Media; const adaptedLogger: Media.Logger = { log: (...args) => webex.logger.log(args.join(' : ')), error: (...args) => webex.logger.error(args.join(' : ')), warn: (...args) => webex.logger.warn(args.join(' : ')), info: (...args) => webex.logger.info(args.join(' : ')), trace: (...args) => webex.logger.trace(args.join(' : ')), debug: (...args) => webex.logger.debug(args.join(' : ')), }; this.mediaEngine.setLogger(adaptedLogger); this.primaryMobiusUris = []; this.backupMobiusUris = []; this.primaryWssMobiusUris = []; this.backupWssMobiusUris = []; this.mobiusClusters = this.webex.internal.services.getMobiusClusters(); this.mobiusHost = ''; this.apiRequest = APIRequest.getInstance({webex: this.webex}); this.registerSessionsListener(); this.registerCallsClearedListener(); } /** * Initializes the `CallingClient` by performing the following steps: * * 1. Retrieves list of servers. * 2. Creates a line. * 3. Sets up network change detection. * * This method should be called once to initialize the `callingClient`. * * @returns A promise that resolves when the initialization is complete. * @ignore */ public async init() { const loggerContext = { file: CALLING_CLIENT_FILE, method: METHODS.INIT, }; log.info(METHOD_START_MESSAGE, loggerContext); // Only for Windows Chromium based browsers we need to do the ICE warmup if (typeof window !== 'undefined' && window?.navigator?.userAgent) { const ua = window.navigator.userAgent; if (ua.toLowerCase().includes('windows')) { log.info('Starting ICE warmup for Windows Chromium based browser', { file: CALLING_CLIENT_FILE, method: 'init', }); try { await windowsChromiumIceWarmup({ iceServers: [], timeoutMs: 1000, }); log.info(`ICE warmup completed`, { file: CALLING_CLIENT_FILE, method: 'init', }); } catch (err) { log.warn(`ICE warmup failed: ${err}`, { file: CALLING_CLIENT_FILE, method: 'init', }); } } } await this.getMobiusServers(); if (this.apiRequest.isSocketEnabled()) { this.apiRequest.registerMobiusSocketConnectionListener({ onConnected: () => this.emit(CALLING_CLIENT_EVENT_KEYS.MOBIUS_SOCKET_CONNECTED), onDisconnected: (reason) => this.emit(CALLING_CLIENT_EVENT_KEYS.MOBIUS_SOCKET_DISCONNECTED, {reason}), }); await this.connectToMobiusSocket(); this.apiRequest.registerMobiusSocketListener(this.handleMobiusAsyncEvent); } // Auto-fetch RTMS domain from service catalog for contact-center flows if ( this.serviceData.indicator === ServiceIndicator.CONTACT_CENTER && !this.serviceData.domain ) { const rtmsDomain = this.getRTMSDomain(); this.serviceData.domain = rtmsDomain; if (this.sdkConfig?.serviceData) { this.sdkConfig.serviceData.domain = this.serviceData.domain; } } if (!isValidServiceDomain(this.serviceData)) { throw new Error('Invalid service domain.'); } await this.createLine(); this.setupNetworkEventListeners(); log.log('CallingClient initialization complete', loggerContext); } /** * Retrieves the RTMS domain from the service catalog for contact-center flows. * * @returns The RTMS domain from catalog when available. */ private getRTMSDomain(): string { log.info('Fetching RTMS domain from service catalog', { file: CALLING_CLIENT_FILE, method: METHODS.GET_RTMS_DOMAIN, }); try { const rtmsURL = this.webex.internal.services.get(WCC_CALLING_RTMS_DOMAIN); const url = new URL(rtmsURL); log.info(`RTMS domain resolved from catalog: ${url.hostname}`, { file: CALLING_CLIENT_FILE, method: METHODS.GET_RTMS_DOMAIN, }); return url.hostname; } catch (error) { log.warn(`Failed to fetch RTMS domain from service catalog: ${error}`, { file: CALLING_CLIENT_FILE, method: METHODS.GET_RTMS_DOMAIN, }); return ''; } } /** * Ping a reliable external endpoint with a short timeout to infer connectivity. */ private async checkNetworkReachability(): Promise { try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 3000); // Using a common connectivity check endpoint that returns 204 with minimal payload. // no-cors mode yields an opaque response but a successful fetch implies reachability. await fetch('https://www.google.com/generate_204', { method: 'GET', cache: 'no-cache', mode: 'no-cors', signal: controller.signal, }); clearTimeout(timeoutId); return true; } catch (error) { log.warn(`Network connectivity probe failed: ${error}`, { file: CALLING_CLIENT_FILE, method: 'pingExternal', }); return false; } } private async checkCallStatus() { const loggerContext = { file: CALLING_CLIENT_FILE, method: 'checkCallStatus', }; const calls = Object.values(this.callManager.getActiveCalls()); for (const call of calls) { call .postStatus() .then(() => { log.info(`Call is active`, loggerContext); /* * Media Renegotiation Possibility if call keepalive succeeds, * for cases like WebRTC disconnect and media inactivity. */ }) .catch((err) => { log.warn(`Call Keepalive failed: ${err}`, loggerContext); call.sendCallStateMachineEvt({type: 'E_SEND_CALL_DISCONNECT'}); }); } } private handleNetworkOffline = async () => { this.networkDownTimestamp = new Date().toISOString(); this.isNetworkDown = !(await this.checkNetworkReachability()); log.warn(`Network has gone down, wait for it to come back up`, { file: CALLING_CLIENT_FILE, method: METHODS.NETWORK_OFFLINE, }); if (this.isNetworkDown) { const line = Object.values(this.lineDict)[0]; line.registration.clearKeepaliveTimer(); } }; // Wondering if we should keep this for timestamp recording purpose private handleNetworkOnline = () => { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.NETWORK_ONLINE, }); this.networkUpTimestamp = new Date().toISOString(); }; private handleMercuryOffline = () => { log.warn(`Mercury down, waiting for connection to be up`, { file: CALLING_CLIENT_FILE, method: METHODS.MERCURY_OFFLINE, }); this.mercuryDownTimestamp = new Date().toISOString(); this.metricManager.submitConnectionMetrics( METRIC_EVENT.CONNECTION_ERROR, CONNECTION_ACTION.MERCURY_DOWN, METRIC_TYPE.BEHAVIORAL, this.mercuryDownTimestamp, this.mercuryUpTimestamp ); }; private handleMercuryOnline = async () => { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.MERCURY_ONLINE, }); this.mercuryUpTimestamp = new Date().toISOString(); if (this.isNetworkDown) { const callCheckInterval = setInterval(async () => { if (!Object.keys(this.callManager.getActiveCalls()).length) { clearInterval(callCheckInterval); const line = Object.values(this.lineDict)[0]; if (line.getStatus() !== RegistrationStatus.IDLE) { this.isNetworkDown = await line.registration.handleConnectionRestoration( this.isNetworkDown ); } else { this.isNetworkDown = false; } } }, NETWORK_FLAP_TIMEOUT); if (Object.keys(this.callManager.getActiveCalls()).length) { await this.checkCallStatus(); } this.metricManager.submitConnectionMetrics( METRIC_EVENT.CONNECTION_ERROR, CONNECTION_ACTION.NETWORK_FLAP, METRIC_TYPE.BEHAVIORAL, this.networkDownTimestamp, this.networkUpTimestamp ); } else { if (Object.keys(this.callManager.getActiveCalls()).length) { await this.checkCallStatus(); } this.metricManager.submitConnectionMetrics( METRIC_EVENT.CONNECTION_ERROR, CONNECTION_ACTION.MERCURY_UP, METRIC_TYPE.BEHAVIORAL, this.mercuryDownTimestamp, this.mercuryUpTimestamp ); } }; private setupNetworkEventListeners(): void { if (typeof window !== 'undefined' && window.addEventListener) { window.addEventListener('online', this.handleNetworkOnline); window.addEventListener('offline', this.handleNetworkOffline); } this.webex.internal.mercury.on('offline', () => { this.handleMercuryOffline(); }); this.webex.internal.mercury.on('online', () => { this.handleMercuryOnline(); }); } /** * Fetches countryCode and region of the client. */ private async getClientRegionInfo(): Promise { let abort; log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.GET_CLIENT_REGION_INFO, }); const regionInfo = {} as RegionInfo; try { const response = await this.webex.request({ uri: `${DISCOVERY_URL}`, method: HTTP_METHODS.GET, addAuthHeader: false, headers: { [SPARK_USER_AGENT]: null, }, }); const clientRegionInfo = response.body as ClientRegionInfo; regionInfo.clientRegion = clientRegionInfo?.clientRegion || ''; regionInfo.countryCode = clientRegionInfo?.countryCode || ''; log.log( `Successfully fetched Client region info: ${regionInfo.clientRegion}, countryCode: ${regionInfo.countryCode}, and response trackingid: ${response?.headers?.trackingid}`, { file: CALLING_CLIENT_FILE, method: METHODS.GET_CLIENT_REGION_INFO, } ); this.metricManager.submitRegionInfoMetric( METRIC_EVENT.MOBIUS_DISCOVERY, MOBIUS_SERVER_ACTION.REGION_INFO, METRIC_TYPE.BEHAVIORAL, this.mobiusHost, clientRegionInfo.clientRegion, clientRegionInfo.countryCode, response?.headers?.trackingid ?? '' ); } catch (err: unknown) { log.error(`Failed to get client region info: ${JSON.stringify(err)}`, { method: METHODS.GET_CLIENT_REGION_INFO, file: CALLING_CLIENT_FILE, }); // eslint-disable-next-line no-await-in-loop abort = await handleCallingClientErrors( err as WebexRequestPayload, (clientError) => { this.metricManager.submitRegistrationMetric( METRIC_EVENT.REGISTRATION_ERROR, REG_ACTION.REGISTER, METRIC_TYPE.BEHAVIORAL, GET_MOBIUS_SERVERS_UTIL, 'UNKNOWN', (err as WebexRequestPayload).headers?.trackingId ?? '', undefined, clientError ); this.emit(CALLING_CLIENT_EVENT_KEYS.ERROR, clientError); }, {method: GET_MOBIUS_SERVERS_UTIL, file: CALLING_CLIENT_FILE} ); regionInfo.clientRegion = ''; regionInfo.countryCode = ''; if (abort) { await uploadLogs(); return regionInfo; } } return regionInfo; } /** * Local method for finding the mobius servers. */ private async getMobiusServers() { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.GET_MOBIUS_SERVERS, }); /* Following operations are performed in a synchronous way -> 1. Get RegionInfo 2. Get Mobius Server with that RegionInfo 3. Check whether Mobius server was found without any error 4. If there is error , we don't need to send registration 5. Otherwise send registration */ let useDefault = false; let clientRegion: string; let countryCode: string; this.mobiusHost = this.webex.internal.services._serviceUrls?.mobius || this.webex.internal.services.get(this.webex.internal.services._activeServices.mobius); if (this.sdkConfig?.discovery?.country && this.sdkConfig?.discovery?.region) { log.log('Updating region and country from the SDK config', { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, }); clientRegion = this.sdkConfig?.discovery?.region; countryCode = this.sdkConfig?.discovery?.country; } else { log.log('Updating region and country through Region discovery', { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, }); const regionInfo = await this.getClientRegionInfo(); clientRegion = regionInfo.clientRegion; countryCode = regionInfo.countryCode; } if (clientRegion && countryCode) { log.log( `Found Region: ${clientRegion} and country: ${countryCode}, going to fetch Mobius server`, { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, } ); /* Attempt the Mobius host from the service link first (when available) and then * fall back to the catalog clusters. Duplicate candidates are removed so the same * host is never queried twice. */ const candidateHosts = [ ...new Set([ ...(this.mobiusHost ? [this.mobiusHost] : []), ...this.mobiusClusters.map((mobius) => mobius.host ? `https://${mobius.host}${API_V1}` : (mobius as unknown as string) ), ]), ]; for (const host of candidateHosts) { this.mobiusHost = host; try { // eslint-disable-next-line no-await-in-loop const response = await this.webex.request({ uri: `${this.mobiusHost}${URL_ENDPOINT}?regionCode=${clientRegion}&countryCode=${countryCode}`, method: HTTP_METHODS.GET, headers: { [CISCO_DEVICE_URL]: this.webex.internal.device.url, [SPARK_USER_AGENT]: CALLING_USER_AGENT, }, service: ALLOWED_SERVICES.MOBIUS, }); log.log( `Mobius Server found for the region. Response trackingId: ${response?.headers?.trackingid}`, { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, } ); const mobiusServers = response.body as MobiusServers; // Metrics for mobius servers this.metricManager.submitMobiusServersMetric( METRIC_EVENT.MOBIUS_DISCOVERY, MOBIUS_SERVER_ACTION.MOBIUS_SERVERS, METRIC_TYPE.BEHAVIORAL, mobiusServers, response?.headers?.trackingid ?? '' ); /* update arrays of Mobius Uris. */ const mobiusUris = filterMobiusUris(mobiusServers, this.mobiusHost); this.primaryMobiusUris = mobiusUris.primary; this.backupMobiusUris = mobiusUris.backup; this.primaryWssMobiusUris = mobiusUris.primaryWss; this.backupWssMobiusUris = mobiusUris.backupWss; log.log( `Final list of Mobius Servers, primary: ${mobiusUris.primary} and backup: ${mobiusUris.backup}`, { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, } ); break; } catch (err: unknown) { log.error(`Failed to get Mobius servers: ${JSON.stringify(err)}`, { method: METHODS.GET_MOBIUS_SERVERS, file: CALLING_CLIENT_FILE, }); // eslint-disable-next-line no-await-in-loop const abort = await handleCallingClientErrors( err as WebexRequestPayload, (clientError) => { this.metricManager.submitRegistrationMetric( METRIC_EVENT.REGISTRATION_ERROR, REG_ACTION.REGISTER, METRIC_TYPE.BEHAVIORAL, GET_MOBIUS_SERVERS_UTIL, 'UNKNOWN', (err as WebexRequestPayload).headers?.trackingId ?? '', undefined, clientError ); this.emit(CALLING_CLIENT_EVENT_KEYS.ERROR, clientError); }, {method: GET_MOBIUS_SERVERS_UTIL, file: CALLING_CLIENT_FILE} ); if (abort) { useDefault = true; // eslint-disable-next-line no-await-in-loop await uploadLogs(); break; } } } } else { /* Setting this to true because region info is possibly undefined */ useDefault = true; } /* Use a default URL if Mobius discovery fails either because of region info failure * or because the discovered Mobius couldn't be reached */ if (useDefault) { log.warn( `Couldn't resolve the region and country code. Defaulting to the catalog entries to discover mobius servers`, { file: CALLING_CLIENT_FILE, method: GET_MOBIUS_SERVERS_UTIL, } ); this.mobiusHost = `https://${this.mobiusClusters[0].host}${API_V1}`; this.primaryMobiusUris = [`${this.mobiusHost}${URL_ENDPOINT}`]; } } /** * Connects to the Mobius WebSocket using WSS URIs discovered during Mobius server discovery. * Attempts primary WSS URIs first, then falls back to backup WSS URIs. * If all attempts fail, logs a warning and continues without a socket connection. */ private async connectToMobiusSocket(): Promise { const loggerContext = { file: CALLING_CLIENT_FILE, method: METHODS.CONNECT_TO_MOBIUS_SOCKET, }; log.info(METHOD_START_MESSAGE, loggerContext); if (!this.primaryWssMobiusUris.length) { log.warn( 'No WSS URIs available from Mobius discovery for primary, skipping socket connection', loggerContext ); return; } // Attempt to establish the socket connection with mobius if primary wss urls are present // If wss url is not present for primary/backup, registration will be attempted with http even when feature flag is enabled for mobius socket for (const wssUri of this.primaryWssMobiusUris) { try { log.log(`Trying primary WSS URI: ${wssUri}`, loggerContext); // eslint-disable-next-line no-await-in-loop await this.apiRequest.connectToMobiusSocket(wssUri); log.log( `Successfully connected to Mobius socket on primary WSS URI: ${wssUri}`, loggerContext ); return; } catch (err: unknown) { log.warn(`Primary WSS URI connection failed for ${wssUri}: ${err}`, loggerContext); } } log.warn('All primary WSS URI connection attempts failed', loggerContext); } private handleMobiusAsyncEvent = async (event?: MobiusAsyncEvent) => { const loggerContext = { file: CALLING_CLIENT_FILE, method: METHODS.HANDLE_MOBIUS_ASYNC_EVENT, }; const eventType = event?.data.eventType; log.trace( `Mobius async event received - eventType: ${eventType ?? 'undefined'}`, loggerContext ); if (!eventType) { log.warn('Dropping unsupported mobius socket payload', loggerContext); return; } if (eventType === MobiusEventType.REGISTRATION_DOWN) { log.warn(`Received ${eventType} event from Mobius.`, loggerContext); const line = Object.values(this.lineDict)[0]; if (!line) { log.warn('No line found, skipping registration down event', loggerContext); return; } await line.registration.handleRegistrationDownEvent(event); this.metricManager.submitMobiusSocketMetric( METRIC_EVENT.MOBIUS_SOCKET_ERROR, MOBIUS_SOCKET_ACTION.REGISTRATION_DOWN, METRIC_TYPE.BEHAVIORAL, undefined, event?.trackingId, undefined, eventType ); return; } this.callManager.dequeueWsEvents(event); log.trace( `Mobius async event dispatched to CallManager - eventType: ${eventType}`, loggerContext ); }; /** * Registers a listener/handler for ALL_CALLS_CLEARED * event emitted by callManager when all the calls * present on sdk are cleaned up. */ private registerCallsClearedListener() { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.REGISTER_CALLS_CLEARED_LISTENER, }); this.callManager.on(CALLING_CLIENT_EVENT_KEYS.ALL_CALLS_CLEARED, this.callsClearedHandler); } /** * Handler registered for ALL_CALLS_CLEARED event emitted by callManager. * * If re-register attempt was deferred earlier due to active call(s), then it * will be attempted here on receiving a notification from callManager that all * calls are cleaned up. */ private callsClearedHandler = async () => { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.CALLS_CLEARED_HANDLER, }); // this is a temporary logic to get registration obj // it will change once we have proper lineId and multiple lines as well const {registration} = Object.values(this.lineDict)[0]; if (!registration.isDeviceRegistered()) { await this.mutex.runExclusive(async () => { if (registration.isReconnectPending()) { log.info('All calls cleared, reconnecting', { file: CALLING_CLIENT_FILE, method: CALLS_CLEARED_HANDLER_UTIL, }); await registration.reconnectOnFailure(CALLS_CLEARED_HANDLER_UTIL); } }); } }; /** * To get the current log Level. * @ignore */ public getLoggingLevel(): LOGGER { return log.getLogLevel(); } /** * To return the `sdkConnector` instance that was used during sdk initialisation. * @ignore */ public getSDKConnector(): ISDKConnector { return this.sdkConnector; } private registerSessionsListener() { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.REGISTER_SESSIONS_LISTENER, }); this.sdkConnector.registerListener( MOBIUS_EVENT_KEYS.CALL_SESSION_EVENT_INCLUSIVE, async (event?: CallSessionEvent) => { if (event && event.data.userSessions.userSessions) { const sessionArr = event?.data.userSessions.userSessions; if (sessionArr.length === 1) { if (sessionArr[0].sessionType !== SessionType.WEBEX_CALLING) { return; } } for (let i = 0; i < sessionArr.length; i += 1) { if (sessionArr[i].sessionType !== SessionType.WEBEX_CALLING) { sessionArr.splice(i, 1); } } this.emit(CALLING_CLIENT_EVENT_KEYS.USER_SESSION_INFO, event as CallSessionEvent); } } ); } /** * Creates line object inside calling client per user * NOTE: currently multiple lines are not supported */ private async createLine(): Promise { log.info(METHOD_START_MESSAGE, { file: CALLING_CLIENT_FILE, method: METHODS.CREATE_LINE, }); // When the Mobius socket is enabled, fall back to the HTTP URIs per group when that // group has no WSS URL, so registration is still attempted (primary and backup are // resolved independently). When the socket is disabled, always use the HTTP URIs. const socketEnabled = this.apiRequest.isSocketEnabled(); const primaryUris = socketEnabled && this.primaryWssMobiusUris.length ? normalizeMobiusUris(this.primaryWssMobiusUris) : this.primaryMobiusUris; const backupUris = socketEnabled && this.backupWssMobiusUris.length ? normalizeMobiusUris(this.backupWssMobiusUris) : this.backupMobiusUris; const line = new Line( this.webex.internal.device.userId, this.webex.internal.device.url, this.mutex, primaryUris, backupUris, this.getLoggingLevel(), this.serviceData, this.sdkConfig?.jwe, this.sdkConfig?.localAudioStream ); this.lineDict[line.lineId] = line; } /** * Retrieves details of all the Line objects belonging to a User * NOTE: currently multiple lines are not supported */ public getLines(): Record { return this.lineDict; } /** * Fetches the list of devices for a given user from Mobius. */ public async getDevices(userId?: string): Promise { const userid = userId || this.webex.internal.device.userId; if (!userid) { throw new Error('userId is required to fetch devices'); } log.info(METHOD_START_MESSAGE, {file: CALLING_CLIENT_FILE, method: METHODS.GET_DEVICES}); const mobiusUrls = [...this.primaryMobiusUris, ...this.backupMobiusUris]; if (mobiusUrls.length === 0) { throw new Error('Mobius URLs are not available'); } let finalError; for (const mobiusUrl of mobiusUrls) { const normalizedMobiusUrl = mobiusUrl.replace(/\/+$/, '/'); const uri = `${normalizedMobiusUrl}${DEVICES_ENDPOINT_RESOURCE}?userid=${encodeURIComponent( userid )}`; try { // eslint-disable-next-line no-await-in-loop const response = (await this.webex.request({ uri, method: HTTP_METHODS.GET, service: ALLOWED_SERVICES.MOBIUS, headers: { [CISCO_DEVICE_URL]: this.webex.internal.device.url, [SPARK_USER_AGENT]: CALLING_USER_AGENT, }, })) as WebexRequestPayload; if (response.statusCode !== 200) { throw new Error(`API call failed with ${response.statusCode}`); } const body = response.body as Devices; // Hydrate registration deviceInfo for deregister/restore flows Object.values(this.lineDict)[0].registration.setDeviceInfo(body); Object.values(this.lineDict)[0].registration.setActiveMobiusUrl(normalizedMobiusUrl); return body.devices ?? []; } catch (error) { finalError = error; } } log.error(`Failed to fetch devices for userId ${userId}: ${JSON.stringify(finalError)}`, { file: CALLING_CLIENT_FILE, method: METHODS.GET_DEVICES, }); throw finalError; } /** * Retrieves call objects for all the active calls present in the client */ public getActiveCalls(): Record { const activeCalls: Record = {}; const calls = this.callManager.getActiveCalls(); Object.keys(calls).forEach((correlationId) => { const call = calls[correlationId]; if (!activeCalls[call.lineId]) { activeCalls[call.lineId] = []; } activeCalls[call.lineId].push(call); }); return activeCalls; } /** * Retrieves call object for the connected call in the client */ public getConnectedCall(): ICall | undefined { let connectCall; const calls = this.callManager.getActiveCalls(); Object.keys(calls).forEach((correlationId) => { if (calls[correlationId].isConnected() && !calls[correlationId].isHeld()) { connectCall = calls[correlationId]; } }); return connectCall; } /** * Indicates whether the Mobius WebSocket transport is currently connected. * * The `MOBIUS_SOCKET_CONNECTED` event is emitted during `init()`, so consumers that * subscribe afterwards may miss it; this lets them reconcile the current state. Returns * `false` when the WebSocket transport is not enabled. */ public isMobiusSocketConnected(): boolean { return this.apiRequest.isSocketEnabled() && this.apiRequest.isSocketConnected(); } /** * Uploads logs to help troubleshoot SDK issues. * * This method collects the current SDK logs including network requests, WebSocket * messages, and client-side events, then securely submits them to Webex's diagnostics * service. The returned tracking ID, feedbackID can be provided to Webex support for faster * issue resolution. * @returns Promise * @throws Error */ public async uploadLogs(): Promise { const result = await uploadLogs({}, true); if (!result) { throw new Error('Failed to upload logs: No response received.'); } return result; } } /** * Create the `CallingClient` instance using the `webex` object and callingSdk `config` * @param webex - A webex instance. * @param config - Config to start the CallingClient with. */ export const createClient = async ( webex: WebexSDK, config?: CallingClientConfig ): Promise => { const callingClientInstance = new CallingClient(webex, config); await callingClientInstance.init(); return callingClientInstance; };