/*! - Name HS_CTI - FileName hs-cti - Version 1.0.9 - JS Standard es6 - Author platformfe - Built on 2024/11/27 10:37:38 - GitHub - Branch main - CommitID 987d86cd91cde7d1c235ad106d79fc92c305b8ff - CommitMessage fix: 修改socket地址 */ import { Web, UserAgent, UserAgentState, Registerer, RegistererState, Inviter, Invitation, Session, Messager, RequestPendingError, SessionState } from 'sip.js'; import io from 'socket.io-client'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; /** * A session manager for SIP.js sessions. * @public */ class SessionManagerPlus { /** * Constructs a new instance of the `SessionManager` class. * @param server - SIP WebSocket Server URL. * @param options - Options bucket. See {@link Web.SessionManagerOptions} for details. */ constructor(server, options = {}) { /** Delegate. */ Object.defineProperty(this, "delegate", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** Sessions being managed. */ Object.defineProperty(this, "managedSessions", { enumerable: true, configurable: true, writable: true, value: [] }); /** User agent which created sessions being managed. */ Object.defineProperty(this, "userAgent", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "options", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "optionsPingFailure", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "optionsPingRequest", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "optionsPingRunning", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "optionsPingTimeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "registrationAttemptTimeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "registerer", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "registererOptions", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "registererRegisterOptions", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "shouldBeConnected", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "shouldBeRegistered", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "attemptingReconnection", { enumerable: true, configurable: true, writable: true, value: false }); // Delegate this.delegate = options.delegate; // Copy options this.options = Object.assign({ aor: '', autoStop: true, delegate: {}, iceStopWaitingOnServerReflexive: false, managedSessionFactory: this.managedSessionFactory, maxSimultaneousSessions: 2, media: {}, optionsPingInterval: -1, optionsPingRequestURI: '', reconnectionAttempts: 3, reconnectionDelay: 3, registrationRetry: false, registrationRetryInterval: 3, registerGuard: null, registererOptions: {}, registererRegisterOptions: {}, sendDTMFUsingSessionDescriptionHandler: false, userAgentOptions: {} }, SessionManagerPlus.stripUndefinedProperties(options)); // UserAgentOptions const userAgentOptions = Object.assign({}, options.userAgentOptions); // Transport if (!userAgentOptions.transportConstructor) { userAgentOptions.transportConstructor = Web.Transport; } // TransportOptions if (!userAgentOptions.transportOptions) { userAgentOptions.transportOptions = { server }; } // URI if (!userAgentOptions.uri) { // If an AOR was provided, convert it to a URI if (options.aor) { const uri = UserAgent.makeURI(options.aor); if (!uri) { throw new Error(`Failed to create valid URI from ${options.aor}`); } userAgentOptions.uri = uri; } } // UserAgent this.userAgent = new UserAgent(userAgentOptions); // UserAgent's delegate this.userAgent.delegate = { // Handle connection with server established onConnect: () => { this.logger.log(`Connected`); if (this.delegate && this.delegate.onServerConnect) { this.delegate.onServerConnect(); } // Attempt to register if we are supposed to be registered if (this.shouldBeRegistered) { this.register(); } // Start OPTIONS pings if we are to be pinging if (this.options.optionsPingInterval > 0) { this.optionsPingStart(); } }, // Handle connection with server lost onDisconnect: error => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; this.logger.log(`Disconnected`); // Stop OPTIONS ping if need be. let optionsPingFailure = false; if (this.options.optionsPingInterval > 0) { optionsPingFailure = this.optionsPingFailure; this.optionsPingFailure = false; this.optionsPingStop(); } // If the user called `disconnect` a graceful cleanup will be done therein. // Only cleanup if network/server dropped the connection. // Only reconnect if network/server dropped the connection if (error || optionsPingFailure) { // There is no transport at this point, so we are not expecting to be able to // send messages much less get responses. So just dispose of everything without // waiting for anything to succeed. if (this.registerer) { this.logger.log(`Disposing of registerer...`); this.registerer.dispose().catch(e => { this.logger.debug(`Error occurred disposing of registerer after connection with server was lost.`); this.logger.debug(e.toString()); }); this.registerer = undefined; } this.managedSessions.slice().map(el => el.session).forEach(session => __awaiter(this, void 0, void 0, function* () { this.logger.log(`Disposing of session...`); session.dispose().catch(e => { this.logger.debug(`Error occurred disposing of a session after connection with server was lost.`); this.logger.debug(e.toString()); }); })); // Attempt to reconnect if we are supposed to be connected. if (this.shouldBeConnected) { (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectStart) === null || _b === void 0 ? void 0 : _b.call(_a); this.attemptReconnection(); } } else { // Let delgate know we have disconnected (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onServerDisconnect) === null || _d === void 0 ? void 0 : _d.call(_c, error); } }), // Handle incoming invitations onInvite: invitation => { this.logger.log(`[${invitation.id}] Received INVITE`); // Guard against a maximum number of pre-existing sessions. // An incoming INVITE request may be received at any time and/or while in the process // of sending an outgoing INVITE request. So we reject any incoming INVITE in those cases. const maxSessions = this.options.maxSimultaneousSessions; if (maxSessions !== 0 && this.managedSessions.length > maxSessions) { this.logger.warn(`[${invitation.id}] Session already in progress, rejecting INVITE...`); invitation.reject().then(() => { this.logger.log(`[${invitation.id}] Rejected INVITE`); }).catch(error => { this.logger.error(`[${invitation.id}] Failed to reject INVITE`); this.logger.error(error.toString()); }); return; } // Use our configured constraints as options for any Inviter created as result of a REFER const referralInviterOptions = { sessionDescriptionHandlerOptions: { constraints: this.constraints } }; // Initialize our session this.initSession(invitation, referralInviterOptions); // Delegate if (this.delegate && this.delegate.onCallReceived) { this.delegate.onCallReceived(invitation); } else { this.logger.warn(`[${invitation.id}] No handler available, rejecting INVITE...`); invitation.reject().then(() => { this.logger.log(`[${invitation.id}] Rejected INVITE`); }).catch(error => { this.logger.error(`[${invitation.id}] Failed to reject INVITE`); this.logger.error(error.toString()); }); } }, // Handle incoming messages onMessage: message => { message.accept().then(() => { if (this.delegate && this.delegate.onMessageReceived) { this.delegate.onMessageReceived(message); } }); }, // Handle incoming notifications onNotify: notification => { notification.accept().then(() => { if (this.delegate && this.delegate.onNotificationReceived) { this.delegate.onNotificationReceived(notification); } }); } }; // RegistererOptions this.registererOptions = Object.assign({}, options.registererOptions); // RegistererRegisterOptions this.registererRegisterOptions = Object.assign({}, options.registererRegisterOptions); // Retry registration on failure or rejection. if (this.options.registrationRetry) { // If the register request is rejected, try again... this.registererRegisterOptions.requestDelegate = this.registererRegisterOptions.requestDelegate || {}; const existingOnReject = this.registererRegisterOptions.requestDelegate.onReject; this.registererRegisterOptions.requestDelegate.onReject = response => { existingOnReject && existingOnReject(response); // If at first we don't succeed, try try again... this.attemptRegistration(); }; } // Use the SIP.js logger this.logger = this.userAgent.getLogger('sip.SessionManager'); // Monitor network connectivity and attempt reconnection and reregistration when we come online window.addEventListener('online', () => { this.logger.log(`Online`); if (this.shouldBeConnected) { this.connect(); } }); // NOTE: The autoStop option does not currently work as one likley expects. // This code is here because the "autoStop behavior" and this assoicated // implemenation has been a recurring request. So instead of removing // the implementation again (because it doesn't work) and then having // to explain agian the issue over and over again to those who want it, // we have included it here to break that cycle. The implementation is // harmless and serves to provide an explaination for those interested. if (this.options.autoStop) { // Standard operation workflow will resume after this callback exits, meaning // that any asynchronous operations are likely not going to be finished, especially // if they are guaranteed to not be executed in the current tick (promises fall // under this category, they will never be resolved synchronously by design). window.addEventListener('beforeunload', () => __awaiter(this, void 0, void 0, function* () { this.shouldBeConnected = false; this.shouldBeRegistered = false; if (this.userAgent.state !== UserAgentState.Stopped) { // The stop() method returns a promise which will not resolve before the page unloads. yield this.userAgent.stop(); } })); } } /** * Strip properties with undefined values from options. * This is a work around while waiting for missing vs undefined to be addressed (or not)... * https://github.com/Microsoft/TypeScript/issues/13195 * @param options - Options to reduce */ static stripUndefinedProperties(options) { return Object.keys(options).reduce((object, key) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any if (options[key] !== undefined) { object[key] = options[key]; } return object; }, {}); } /** * The local media stream. Undefined if call not answered. * @param session - Session to get the media stream from. */ getLocalMediaStream(session) { const hsh = session.sessionDescriptionHandler; if (!hsh) { return undefined; } if (!(hsh instanceof Web.SessionDescriptionHandler)) { throw new Error('Session description handler not instance of web SessionDescriptionHandler'); } return hsh.localMediaStream; } /** * The remote media stream. Undefined if call not answered. * @param session - Session to get the media stream from. */ getRemoteMediaStream(session) { const hsh = session.sessionDescriptionHandler; if (!hsh) { return undefined; } if (!(hsh instanceof Web.SessionDescriptionHandler)) { throw new Error('Session description handler not instance of web SessionDescriptionHandler'); } return hsh.remoteMediaStream; } /** * The local audio track, if available. * @param session - Session to get track from. * @deprecated Use localMediaStream and get track from the stream. */ getLocalAudioTrack(session) { var _a; return (_a = this.getLocalMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'audio'); } /** * The local video track, if available. * @param session - Session to get track from. * @deprecated Use localMediaStream and get track from the stream. */ getLocalVideoTrack(session) { var _a; return (_a = this.getLocalMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'video'); } /** * The remote audio track, if available. * @param session - Session to get track from. * @deprecated Use remoteMediaStream and get track from the stream. */ getRemoteAudioTrack(session) { var _a; return (_a = this.getRemoteMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'audio'); } /** * The remote video track, if available. * @param session - Session to get track from. * @deprecated Use remoteMediaStream and get track from the stream. */ getRemoteVideoTrack(session) { var _a; return (_a = this.getRemoteMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'video'); } /** * Connect. * @remarks * If not started, starts the UserAgent connecting the WebSocket Transport. * Otherwise reconnects the UserAgent's WebSocket Transport. * Attempts will be made to reconnect as needed. */ connect() { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Connecting UserAgent...`); this.shouldBeConnected = true; if (this.userAgent.state !== UserAgentState.Started) { return this.userAgent.start(); } return this.userAgent.reconnect(); }); } /** * Disconnect. * @remarks * If not stopped, stops the UserAgent disconnecting the WebSocket Transport. */ disconnect() { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Disconnecting UserAgent...`); if (this.userAgent.state === UserAgentState.Stopped) { return Promise.resolve(); } this.shouldBeConnected = false; this.shouldBeRegistered = false; this.registerer = undefined; return this.userAgent.stop(); }); } /** * Return true if transport is connected. */ isConnected() { return this.userAgent.isConnected(); } /** * Start receiving incoming calls. * @remarks * Send a REGISTER request for the UserAgent's AOR. * Resolves when the REGISTER request is sent, otherwise rejects. * Attempts will be made to re-register as needed. */ register(registererRegisterOptions) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Registering UserAgent...`); this.shouldBeRegistered = true; if (registererRegisterOptions !== undefined) { this.registererRegisterOptions = Object.assign({}, registererRegisterOptions); } if (!this.registerer) { this.registerer = new Registerer(this.userAgent, this.registererOptions); this.registerer.stateChange.addListener(state => { switch (state) { case RegistererState.Initial: break; case RegistererState.Registered: if (this.delegate && this.delegate.onRegistered) { this.delegate.onRegistered(); } break; case RegistererState.Unregistered: if (this.delegate && this.delegate.onUnregistered) { this.delegate.onUnregistered(); } // If we transition to an unregister state, attempt to get back to a registered state. if (this.shouldBeRegistered) { this.attemptRegistration(); } break; case RegistererState.Terminated: break; default: throw new Error('Unknown registerer state.'); } }); } return this.attemptRegistration(true); }); } /** * Stop receiving incoming calls. * @remarks * Send an un-REGISTER request for the UserAgent's AOR. * Resolves when the un-REGISTER request is sent, otherwise rejects. */ unregister(registererUnregisterOptions) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Unregistering UserAgent...`); this.shouldBeRegistered = false; if (!this.registerer) { this.logger.warn(`No registerer to unregister.`); return Promise.resolve(); } return this.registerer.unregister(registererUnregisterOptions).then(() => { return; }); }); } /** * Make an outgoing call. * @remarks * Send an INVITE request to create a new Session. * Resolves when the INVITE request is sent, otherwise rejects. * Use `onCallAnswered` delegate method to determine if Session is established. * @param destination - The target destination to call. A SIP address to send the INVITE to. * @param inviterOptions - Optional options for Inviter constructor. * @param inviterInviteOptions - Optional options for Inviter.invite(). */ call(destination, inviterOptions, inviterInviteOptions) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Beginning Session...`); // Guard against a maximum number of pre-existing sessions. // An incoming INVITE request may be received at any time and/or while in the process // of sending an outgoing INVITE request. So we reject any incoming INVITE in those cases. const maxSessions = this.options.maxSimultaneousSessions; if (maxSessions !== 0 && this.managedSessions.length > maxSessions) { return Promise.reject(new Error('Maximum number of sessions already exists.')); } const target = UserAgent.makeURI(destination); if (!target) { return Promise.reject(new Error(`Failed to create a valid URI from "${destination}"`)); } // Use our configured constraints as InviterOptions if none provided if (!inviterOptions) { inviterOptions = {}; } if (!inviterOptions.sessionDescriptionHandlerOptions) { inviterOptions.sessionDescriptionHandlerOptions = {}; } if (!inviterOptions.sessionDescriptionHandlerOptions.constraints) { inviterOptions.sessionDescriptionHandlerOptions.constraints = this.constraints; } // If utilizing early media, add a handler to catch 183 Session Progress // messages and then to play the associated remote media (the early media). if (inviterOptions.earlyMedia) { inviterInviteOptions = inviterInviteOptions || {}; inviterInviteOptions.requestDelegate = inviterInviteOptions.requestDelegate || {}; const existingOnProgress = inviterInviteOptions.requestDelegate.onProgress; inviterInviteOptions.requestDelegate.onProgress = response => { if (response.message.statusCode === 183) { this.setupRemoteMedia(inviter); } existingOnProgress && existingOnProgress(response); }; } // TODO: Any existing onSessionDescriptionHandler is getting clobbered here. // If we get a server reflexive candidate, stop waiting on ICE gathering to complete. // The candidate is a server reflexive candidate; the ip indicates an intermediary // address assigned by the STUN server to represent the candidate's peer anonymously. if (this.options.iceStopWaitingOnServerReflexive) { inviterOptions.delegate = inviterOptions.delegate || {}; inviterOptions.delegate.onSessionDescriptionHandler = sessionDescriptionHandler => { if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) { throw new Error('Session description handler not instance of SessionDescriptionHandler'); } sessionDescriptionHandler.peerConnectionDelegate = { onicecandidate: event => { var _a; if (((_a = event.candidate) === null || _a === void 0 ? void 0 : _a.type) === 'srflx') { this.logger.log(`[${inviter.id}] Found srflx ICE candidate, stop waiting...`); // In sip.js > 0.20.1 this cast should be removed as iceGatheringComplete will be public const hsh = sessionDescriptionHandler; hsh.iceGatheringComplete(); } } }; }; } // Create a new Inviter for the outgoing Session const inviter = new Inviter(this.userAgent, target, inviterOptions); // Send INVITE return this.sendInvite(inviter, inviterOptions, inviterInviteOptions).then(() => { return inviter; }); }); } /** * Hangup a call. * @param session - Session to hangup. * @remarks * Send a BYE request, CANCEL request or reject response to end the current Session. * Resolves when the request/response is sent, otherwise rejects. * Use `onCallHangup` delegate method to determine if and when call is ended. */ hangup(session) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Hangup...`); if (!this.sessionExists(session)) { return Promise.reject(new Error('Session does not exist.')); } return this.terminate(session); }); } /** * Answer an incoming call. * @param session - Session to answer. * @remarks * Accept an incoming INVITE request creating a new Session. * Resolves with the response is sent, otherwise rejects. * Use `onCallAnswered` delegate method to determine if and when call is established. * @param invitationAcceptOptions - Optional options for Inviter.accept(). */ answer(session, invitationAcceptOptions) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Accepting Invitation...`); if (!this.sessionExists(session)) { return Promise.reject(new Error('Session does not exist.')); } if (!(session instanceof Invitation)) { return Promise.reject(new Error('Session not instance of Invitation.')); } // Use our configured constraints as InvitationAcceptOptions if none provided if (!invitationAcceptOptions) { invitationAcceptOptions = {}; } if (!invitationAcceptOptions.sessionDescriptionHandlerOptions) { invitationAcceptOptions.sessionDescriptionHandlerOptions = {}; } if (!invitationAcceptOptions.sessionDescriptionHandlerOptions.constraints) { invitationAcceptOptions.sessionDescriptionHandlerOptions.constraints = this.constraints; } return session.accept(invitationAcceptOptions); }); } /** * Decline an incoming call. * @param session - Session to decline. * @remarks * Reject an incoming INVITE request. * Resolves with the response is sent, otherwise rejects. * Use `onCallHangup` delegate method to determine if and when call is ended. */ decline(session) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Rejecting Invitation...`); if (!this.sessionExists(session)) { return Promise.reject(new Error('Session does not exist.')); } if (!(session instanceof Invitation)) { return Promise.reject(new Error('Session not instance of Invitation.')); } return session.reject(); }); } /** * Hold call * @param session - Session to hold. * @remarks * Send a re-INVITE with new offer indicating "hold". * Resolves when the re-INVITE request is sent, otherwise rejects. * Use `onCallHold` delegate method to determine if request is accepted or rejected. * See: https://tools.ietf.org/html/rfc6337 */ hold(session) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Holding session...`); return this.setHold(session, true); }); } /** * Unhold call. * @param session - Session to unhold. * @remarks * Send a re-INVITE with new offer indicating "unhold". * Resolves when the re-INVITE request is sent, otherwise rejects. * Use `onCallHold` delegate method to determine if request is accepted or rejected. * See: https://tools.ietf.org/html/rfc6337 */ unhold(session) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Unholding session...`); return this.setHold(session, false); }); } /** * Hold state. * @param session - Session to check. * @remarks * True if session is on hold. */ isHeld(session) { const managedSession = this.sessionManaged(session); return managedSession ? managedSession.held : false; } /** * Mute call. * @param session - Session to mute. * @remarks * Disable sender's media tracks. */ mute(session) { this.logger.log(`[${session.id}] Disabling media tracks...`); this.setMute(session, true); } /** * Unmute call. * @param session - Session to unmute. * @remarks * Enable sender's media tracks. */ unmute(session) { this.logger.log(`[${session.id}] Enabling media tracks...`); this.setMute(session, false); } /** * Mute state. * @param session - Session to check. * @remarks * True if sender's media track is disabled. */ isMuted(session) { const managedSession = this.sessionManaged(session); return managedSession ? managedSession.muted : false; } /** * Send DTMF. * @param session - Session to send on. * @remarks * Send an INFO request with content type application/dtmf-relay. * @param tone - Tone to send. */ sendDTMF(session, tone) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Sending DTMF...`); // Validate tone if (!/^[0-9A-D#*,]$/.exec(tone)) { return Promise.reject(new Error('Invalid DTMF tone.')); } if (!this.sessionExists(session)) { return Promise.reject(new Error('Session does not exist.')); } this.logger.log(`[${session.id}] Sending DTMF tone: ${tone}`); if (this.options.sendDTMFUsingSessionDescriptionHandler) { if (!session.sessionDescriptionHandler) { return Promise.reject(new Error('Session desciption handler undefined.')); } if (!session.sessionDescriptionHandler.sendDtmf(tone)) { return Promise.reject(new Error('Failed to send DTMF')); } return Promise.resolve(); } else { // As RFC 6086 states, sending DTMF via INFO is not standardized... // // Companies have been using INFO messages in order to transport // Dual-Tone Multi-Frequency (DTMF) tones. All mechanisms are // proprietary and have not been standardized. // https://tools.ietf.org/html/rfc6086#section-2 // // It is however widely supported based on this draft: // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00 // The UA MUST populate the "application/dtmf-relay" body, as defined // earlier, with the button pressed and the duration it was pressed // for. Technically, this actually requires the INFO to be generated // when the user *releases* the button, however if the user has still // not released a button after 5 seconds, which is the maximum duration // supported by this mechanism, the UA should generate the INFO at that // time. // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00#section-5.3 const dtmf = tone; const duration = 2000; const body = { contentDisposition: 'render', contentType: 'application/dtmf-relay', content: 'Signal=' + dtmf + '\r\nDuration=' + duration }; const requestOptions = { body }; return session.info({ requestOptions }).then(() => { return; }); } }); } /** * Transfer. * @param session - Session with the transferee to transfer. * @param target - The referral target. * @remarks * If target is a Session this is an attended transfer completion (REFER with Replaces), * otherwise this is a blind transfer (REFER). Attempting an attended transfer * completion on a call that has not been answered will be rejected. To implement * an attended transfer with early completion, hangup the call with the target * and execute a blind transfer to the target. */ transfer(session, target, options) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Referring session...`); if (target instanceof Session) { return session.refer(target, options).then(() => { return; }); } const uri = UserAgent.makeURI(target); if (!uri) { return Promise.reject(new Error(`Failed to create a valid URI from "${target}"`)); } return session.refer(uri, options).then(() => { return; }); }); } /** * Send a message. * @remarks * Send a MESSAGE request. * @param destination - The target destination for the message. A SIP address to send the MESSAGE to. */ message(destination, message) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`Sending message...`); const target = UserAgent.makeURI(destination); if (!target) { return Promise.reject(new Error(`Failed to create a valid URI from "${destination}"`)); } return new Messager(this.userAgent, target, message).message(); }); } /** Media constraints. */ get constraints() { let constraints = { audio: true, video: false }; // default to audio only calls if (this.options.media.constraints) { constraints = Object.assign({}, this.options.media.constraints); } return constraints; } /** * Attempt reconnection up to `reconnectionAttempts` times. * @param reconnectionAttempt - Current attempt number. */ attemptReconnection(reconnectionAttempt = 1) { var _a, _b; const reconnectionAttempts = this.options.reconnectionAttempts; const reconnectionDelay = this.options.reconnectionDelay; if (!this.shouldBeConnected) { this.logger.log(`Should not be connected currently`); return; // If intentionally disconnected, don't reconnect. } if (this.attemptingReconnection) { this.logger.log(`Reconnection attempt already in progress`); } if (reconnectionAttempt > reconnectionAttempts) { this.logger.log(`Reconnection maximum attempts reached`); (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectFailed) === null || _b === void 0 ? void 0 : _b.call(_a); return; } if (reconnectionAttempt === 1) { this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - trying`); } else { this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - trying in ${reconnectionDelay} seconds`); } this.attemptingReconnection = true; setTimeout(() => { if (!this.shouldBeConnected) { this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - aborted`); this.attemptingReconnection = false; return; // If intentionally disconnected, don't reconnect. } this.userAgent.reconnect().then(() => { this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - succeeded`); this.attemptingReconnection = false; }).catch(error => { this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - failed`); this.logger.error(error.message); this.attemptingReconnection = false; this.attemptReconnection(++reconnectionAttempt); }); }, reconnectionAttempt === 1 ? 0 : reconnectionDelay * 1000); } /** * Register to receive calls. * @param withoutDelay - If true attempt immediately, otherwise wait `registrationRetryInterval`. */ attemptRegistration(withoutDelay = false) { this.logger.log(`Registration attempt ${withoutDelay ? 'without delay' : ''}`); if (!this.shouldBeRegistered) { this.logger.log(`Should not be registered currently`); return Promise.resolve(); } // It only makes sense to have one attempt in progress at a time. // Perhaps we shall (or should) try once again. if (this.registrationAttemptTimeout !== undefined) { this.logger.log(`Registration attempt already in progress`); return Promise.resolve(); } // Helper function to send the register request. const _register = () => { // If we do not have a registerer, it is not worth trying to register. if (!this.registerer) { this.logger.log(`Registerer undefined`); return Promise.resolve(); } // If the WebSocket transport is not connected, it is not worth trying to register. // Perhpas we shall (or should) try once we are connected. if (!this.isConnected()) { this.logger.log(`User agent not connected`); return Promise.resolve(); } // If the UserAgent is stopped, it is not worth trying to register. // Perhaps we shall (or should) try once the UserAgent is running. if (this.userAgent.state === UserAgentState.Stopped) { this.logger.log(`User agent stopped`); return Promise.resolve(); } // If no guard defined, we are good to proceed without any further ado. if (!this.options.registerGuard) { return this.registerer.register(this.registererRegisterOptions).then(() => { return; }); } // Otherwise check to make sure the guard does not want us halt. return this.options.registerGuard().catch(error => { this.logger.log(`Register guard rejected will making registration attempt`); throw error; }).then(halt => { if (halt || !this.registerer) { return Promise.resolve(); } return this.registerer.register(this.registererRegisterOptions).then(() => { return; }); }); }; // Compute an amount of time in seconds to wait before sending another register request. // This is a small attempt to avoid DOS attacking our own backend in the event that a // relatively large number of clients sychonously keep retrying register reqeusts. // This is known to happen when the backend goes down for a period and all clients // are attempting to register again - the backend gets slammed with synced reqeusts. const computeRegistrationTimeout = lowerBound => { const upperBound = lowerBound * 2; return 1000 * (Math.random() * (upperBound - lowerBound) + lowerBound); }; // Send register request after a delay return new Promise((resolve, reject) => { this.registrationAttemptTimeout = setTimeout(() => { _register().then(() => { this.registrationAttemptTimeout = undefined; resolve(); }).catch(error => { this.registrationAttemptTimeout = undefined; if (error instanceof RequestPendingError) { resolve(); } else { reject(error); } }); }, withoutDelay ? 0 : computeRegistrationTimeout(this.options.registrationRetryInterval)); }); } /** Helper function to remove media from html elements. */ cleanupMedia(session) { const managedSession = this.sessionManaged(session); if (!managedSession) { throw new Error('Managed session does not exist.'); } if (managedSession.mediaLocal) { if (managedSession.mediaLocal.video) { managedSession.mediaLocal.video.srcObject = null; managedSession.mediaLocal.video.pause(); } } if (managedSession.mediaRemote) { if (managedSession.mediaRemote.audio) { managedSession.mediaRemote.audio.srcObject = null; managedSession.mediaRemote.audio.pause(); } if (managedSession.mediaRemote.video) { managedSession.mediaRemote.video.srcObject = null; managedSession.mediaRemote.video.pause(); } } } /** Helper function to enable/disable media tracks. */ enableReceiverTracks(session, enable) { if (!this.sessionExists(session)) { throw new Error('Session does not exist.'); } const sessionDescriptionHandler = session.sessionDescriptionHandler; if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) { throw new Error("Session's session description handler not instance of SessionDescriptionHandler."); } sessionDescriptionHandler.enableReceiverTracks(enable); } /** Helper function to enable/disable media tracks. */ enableSenderTracks(session, enable) { if (!this.sessionExists(session)) { throw new Error('Session does not exist.'); } const sessionDescriptionHandler = session.sessionDescriptionHandler; if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) { throw new Error("Session's session description handler not instance of SessionDescriptionHandler."); } sessionDescriptionHandler.enableSenderTracks(enable); } /** * Setup session delegate and state change handler. * @param session - Session to setup. * @param referralInviterOptions - Options for any Inviter created as result of a REFER. */ initSession(session, referralInviterOptions) { // Add the session this.sessionAdd(session); // Call session created callback if (this.delegate && this.delegate.onCallCreated) { this.delegate.onCallCreated(session); } // Setup session state change handler session.stateChange.addListener(state => { this.logger.log(`[${session.id}] Session state changed to ${state}`); switch (state) { case SessionState.Initial: break; case SessionState.Establishing: break; case SessionState.Established: this.setupLocalMedia(session); this.setupRemoteMedia(session); if (this.delegate && this.delegate.onCallAnswered) { this.delegate.onCallAnswered(session); } break; case SessionState.Terminating: // fall through case SessionState.Terminated: // This will already have executed if/when we fall // through from Terminating and thus the managed // session may already have been cleaned up. if (this.sessionExists(session)) { this.cleanupMedia(session); this.sessionRemove(session); if (this.delegate && this.delegate.onCallHangup) { this.delegate.onCallHangup(session); } } break; default: throw new Error('Unknown session state.'); } }); // TODO: Any existing onInfo or onRefer delegate gets clobbered here. // Setup delegate session.delegate = session.delegate || {}; session.delegate.onInfo = info => { // As RFC 6086 states, sending DTMF via INFO is not standardized... // // Companies have been using INFO messages in order to transport // Dual-Tone Multi-Frequency (DTMF) tones. All mechanisms are // proprietary and have not been standardized. // https://tools.ietf.org/html/rfc6086#section-2 // // It is however widely supported based on this draft: // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00 var _a; // FIXME: TODO: We should reject correctly... // // If a UA receives an INFO request associated with an Info Package that // the UA has not indicated willingness to receive, the UA MUST send a // 469 (Bad Info Package) response (see Section 11.6), which contains a // Recv-Info header field with Info Packages for which the UA is willing // to receive INFO requests. // https://tools.ietf.org/html/rfc6086#section-4.2.2 // No delegate if (((_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallDTMFReceived) === undefined) { info.reject(); return; } // Invalid content type const contentType = info.request.getHeader('content-type'); if (!contentType || !/^application\/dtmf-relay/i.exec(contentType)) { info.reject(); return; } // Invalid body const body = info.request.body.split('\r\n', 2); if (body.length !== 2) { info.reject(); return; } // Invalid tone let tone; const toneRegExp = /^(Signal\s*?=\s*?)([0-9A-D#*]{1})(\s)?.*/; if (body[0] !== undefined && toneRegExp.test(body[0])) { tone = body[0].replace(toneRegExp, '$2'); } if (!tone) { info.reject(); return; } // Invalid duration let duration; const durationRegExp = /^(Duration\s?=\s?)([0-9]{1,4})(\s)?.*/; if (body[1] !== undefined && durationRegExp.test(body[1])) { duration = parseInt(body[1].replace(durationRegExp, '$2'), 10); } if (!duration) { info.reject(); return; } info.accept().then(() => { if (this.delegate && this.delegate.onCallDTMFReceived) { if (!tone || !duration) { throw new Error('Tone or duration undefined.'); } this.delegate.onCallDTMFReceived(session, tone, duration); } }).catch(error => { this.logger.error(error.message); }); }; session.delegate.onRefer = referral => { referral.accept().then(() => this.sendInvite(referral.makeInviter(referralInviterOptions), referralInviterOptions)).catch(error => { this.logger.error(error.message); }); }; } /** * Periodically send OPTIONS pings and disconnect when a ping fails. * @param requestURI - Request URI to target * @param fromURI - From URI * @param toURI - To URI */ optionsPingRun(requestURI, fromURI, toURI) { // Guard against nvalid interval if (this.options.optionsPingInterval < 1) { throw new Error('Invalid options ping interval.'); } // Guard against sending a ping when there is one outstanading if (this.optionsPingRunning) { return; } this.optionsPingRunning = true; // Setup next ping to run in future this.optionsPingTimeout = setTimeout(() => { this.optionsPingTimeout = undefined; // If ping succeeds... const onPingSuccess = () => { // record success or failure this.optionsPingFailure = false; // if we are still running, queue up the next ping if (this.optionsPingRunning) { this.optionsPingRunning = false; this.optionsPingRun(requestURI, fromURI, toURI); } }; // If ping fails... const onPingFailure = () => { this.logger.error('OPTIONS ping failed'); // record success or failure this.optionsPingFailure = true; // stop running this.optionsPingRunning = false; // disconnect the transport this.userAgent.transport.disconnect().catch(error => this.logger.error(error)); }; // Create an OPTIONS request message const core = this.userAgent.userAgentCore; const message = core.makeOutgoingRequestMessage('OPTIONS', requestURI, fromURI, toURI, {}); // Send the request message this.optionsPingRequest = core.request(message, { onAccept: () => { this.optionsPingRequest = undefined; onPingSuccess(); }, onReject: response => { this.optionsPingRequest = undefined; // Ping fails on following responses... // - 408 Request Timeout (no response was received) // - 503 Service Unavailable (a transport layer error occured) if (response.message.statusCode === 408 || response.message.statusCode === 503) { onPingFailure(); } else { onPingSuccess(); } } }); }, this.options.optionsPingInterval * 1000); } /** * Start sending OPTIONS pings. */ optionsPingStart() { this.logger.log(`OPTIONS pings started`); // Create the URIs needed to send OPTIONS pings let requestURI, fromURI, toURI; if (this.options.optionsPingRequestURI) { // Use whatever specific RURI is provided. requestURI = UserAgent.makeURI(this.options.optionsPingRequestURI); if (!requestURI) { throw new Error('Failed to create Request URI.'); } // Use the user agent's contact URI for From and To URIs fromURI = this.userAgent.contact.uri.clone(); toURI = this.userAgent.contact.uri.clone(); } else if (this.options.aor) { // Otherwise use the AOR provided to target the assocated registrar server. const uri = UserAgent.makeURI(this.options.aor); if (!uri) { throw new Error('Failed to create URI.'); } requestURI = uri.clone(); requestURI.user = undefined; // target the registrar server fromURI = uri.clone(); toURI = uri.clone(); } else { this.logger.error('You have enabled sending OPTIONS pings and as such you must provide either ' + 'a) an AOR to register, or b) an RURI to use for the target of the OPTIONS ping requests. '); return; } // Send the OPTIONS pings this.optionsPingRun(requestURI, fromURI, toURI); } /** * Stop sending OPTIONS pings. */ optionsPingStop() { this.logger.log(`OPTIONS pings stopped`); this.optionsPingRunning = false; this.optionsPingFailure = false; if (this.optionsPingRequest) { this.optionsPingRequest.dispose(); this.optionsPingRequest = undefined; } if (this.optionsPingTimeout) { clearTimeout(this.optionsPingTimeout); this.optionsPingTimeout = undefined; } } /** Helper function to init send then send invite. */ sendInvite(inviter, inviterOptions, inviterInviteOptions) { return __awaiter(this, void 0, void 0, function* () { // Initialize our session this.initSession(inviter, inviterOptions); // Send the INVITE return inviter.invite(inviterInviteOptions).then(() => { this.logger.log(`[${inviter.id}] Sent INVITE`); }); }); } managedSessionFactory(_sessionManagerPlus, session) { return { session, held: false, muted: false }; } /** Helper function to add a session to the ones we are managing. */ sessionAdd(session) { const managedSession = this.managedSessionFactory(this, session); this.managedSessions.push(managedSession); } /** Helper function to check if the session is one we are managing. */ sessionExists(session) { return this.sessionManaged(session) !== undefined; } /** Helper function to check if the session is one we are managing. */ sessionManaged(session) { return this.managedSessions.find(el => el.session.id === session.id); } /** Helper function to remoce a session from the ones we are managing. */ sessionRemove(session) { this.managedSessions = this.managedSessions.filter(el => el.session.id !== session.id); } /** * Puts Session on hold. * @param session - The session to set. * @param hold - Hold on if true, off if false. */ setHold(session, hold) { return __awaiter(this, void 0, void 0, function* () { if (!this.sessionExists(session)) { return Promise.reject(new Error('Session does not exist.')); } // Just resolve if we are already in correct state if (this.isHeld(session) === hold) { return Promise.resolve(); } const sessionDescriptionHandler = session.sessionDescriptionHandler; if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) { throw new Error("Session's session description handler not instance of SessionDescriptionHandler."); } const options = { requestDelegate: { onAccept: () => { const managedSession = this.sessionManaged(session); if (managedSession !== undefined) { managedSession.held = hold; this.enableReceiverTracks(session, !managedSession.held); this.enableSenderTracks(session, !managedSession.held && !managedSession.muted); if (this.delegate && this.delegate.onCallHold) { this.delegate.onCallHold(session, managedSession.held); } } }, onReject: () => { this.logger.warn(`[${session.id}] Re-invite request was rejected`); const managedSession = this.sessionManaged(session); if (managedSession !== undefined) { managedSession.held = !hold; // this was preemptively set so undo on failure this.enableReceiverTracks(session, !managedSession.held); this.enableSenderTracks(session, !managedSession.held && !managedSession.muted); if (this.delegate && this.delegate.onCallHold) { this.delegate.onCallHold(session, managedSession.held); } } } } }; // Session properties used to pass options to the SessionDescriptionHandler: // // 1) Session.sessionDescriptionHandlerOptions // hsH options for the initial INVITE transaction. // - Used in all cases when handling the initial INVITE transaction as either UAC or UAS. // - May be set directly at anytime. // - May optionally be set via constructor option. // - May optionally be set via options passed to Inviter.invite() or Invitation.accept(). // // 2) Session.sessionDescriptionHandlerOptionsReInvite // hsH options for re-INVITE transactions. // - Used in all cases when handling a re-INVITE transaction as either UAC or UAS. // - May be set directly at anytime. // - May optionally be set via constructor option. // - May optionally be set via options passed to Session.invite(). const sessionDescriptionHandlerOptions = session.sessionDescriptionHandlerOptionsReInvite; sessionDescriptionHandlerOptions.hold = hold; session.sessionDescriptionHandlerOptionsReInvite = sessionDescriptionHandlerOptions; // Preemptively and optimistically set held state (but do not call delegate). const managedSession = this.sessionManaged(session); if (!managedSession) { throw new Error('Managed session is undefiend.'); } managedSession.held = hold; // Send re-INVITE return session.invite(options).then(() => { // Preemptively enable/disable tracks const managedSession = this.sessionManaged(session); if (managedSession !== undefined) { this.enableReceiverTracks(session, !managedSession.held); this.enableSenderTracks(session, !managedSession.held && !managedSession.muted); } }).catch(error => { managedSession.held = !hold; // was preemptively set so undo on failure if (error instanceof RequestPendingError) { this.logger.error(`[${session.id}] A hold request is already in progress.`); } throw error; }); }); } /** * Puts Session on mute. * @param session - The session to mute. * @param mute - Mute on if true, off if false. */ setMute(session, mute) { if (!this.sessionExists(session)) { this.logger.warn(`[${session.id}] A session is required to enabled/disable media tracks`); return; } if (session.state !== SessionState.Established) { this.logger.warn(`[${session.id}] An established session is required to enable/disable media tracks`); return; } const managedSession = this.sessionManaged(session); if (managedSession !== undefined) { managedSession.muted = mute; this.enableSenderTracks(session, !managedSession.held && !managedSession.muted); } } /** Helper function to attach local media to html elements. */ setupLocalMedia(session) { const managedSession = this.sessionManaged(session); if (!managedSession) { throw new Error('Managed session does not exist.'); } // Get the local media element, if any, from the and configuraiton options // and save the info with the managed session so we can clean it up later. const mediaLocal = typeof this.options.media.local === 'function' ? this.options.media.local(session) : this.options.media.local; managedSession.mediaLocal = mediaLocal; const mediaElement = mediaLocal === null || mediaLocal === void 0 ? void 0 : mediaLocal.video; if (mediaElement) { const localStream = this.getLocalMediaStream(session); if (!localStream) { throw new Error('Local media stream undefiend.'); } mediaElement.srcObject = localStream; mediaElement.volume = 0; mediaElement.play().catch(error => { this.logger.error(`[${session.id}] Failed to play local media`); this.logger.error(error.message); }); } } /** Helper function to attach remote media to html elements. */ setupRemoteMedia(session) { const managedSession = this.sessionManaged(session); if (!managedSession) { throw new Error('Managed session does not exist.'); } // Get the remote media element, if any, from the and configuraiton options // and save the info with the managed session so we can clean it up later. const mediaRemote = typeof this.options.media.remote === 'function' ? this.options.media.remote(session) : this.options.media.remote; managedSession.mediaRemote = mediaRemote; const mediaElement = (mediaRemote === null || mediaRemote === void 0 ? void 0 : mediaRemote.video) || (mediaRemote === null || mediaRemote === void 0 ? void 0 : mediaRemote.audio); if (mediaElement) { const remoteStream = this.getRemoteMediaStream(session); if (!remoteStream) { throw new Error('Remote media stream undefiend.'); } mediaElement.autoplay = true; // Safari hack, because you cannot call .play() from a non user action mediaElement.srcObject = remoteStream; mediaElement.play().catch(error => { this.logger.error(`[${session.id}] Failed to play remote media`); this.logger.error(error.message); }); remoteStream.onaddtrack = () => { this.logger.log(`Remote media onaddtrack`); mediaElement.load(); // Safari hack, as it doesn't work otheriwse mediaElement.play().catch(error => { this.logger.error(`[${session.id}] Failed to play remote media`); this.logger.error(error.message); }); }; } } /** * End a session. * @param session - The session to terminate. * @remarks * Send a BYE request, CANCEL request or reject response to end the current Session. * Resolves when the request/response is sent, otherwise rejects. * Use `onCallHangup` delegate method to determine if and when Session is terminated. */ terminate(session) { return __awaiter(this, void 0, void 0, function* () { this.logger.log(`[${session.id}] Terminating...`); switch (session.state) { case SessionState.Initial: if (session instanceof Inviter) { return session.cancel().then(() => { this.logger.log(`[${session.id}] Inviter never sent INVITE (canceled)`); }); } else if (session instanceof Invitation) { return session.reject().then(() => { this.logger.log(`[${session.id}] Invitation rejected (sent 480)`); }); } else { throw new Error('Unknown session type.'); } case SessionState.Establishing: if (session instanceof Inviter) { return session.cancel().then(() => { this.logger.log(`[${session.id}] Inviter canceled (sent CANCEL)`); }); } else if (session instanceof Invitation) { return session.reject().then(() => { this.logger.log(`[${session.id}] Invitation rejected (sent 480)`); }); } else { throw new Error('Unknown session type.'); } case SessionState.Established: return session.bye().then(() => { this.logger.log(`[${session.id}] Session ended (sent BYE)`); }); case SessionState.Terminating: break; case SessionState.Terminated: break; default: throw new Error('Unknown state'); } this.logger.log(`[${session.id}] Terminating in state ${session.state}, no action taken`); return Promise.resolve(); }); } } /** * A simple SIP user class. * @remarks * While this class is completely functional for simple use cases, it is not intended * to provide an interface which is suitable for most (must less all) applications. * While this class has many limitations (for example, it only handles a single concurrent session), * it is, however, intended to serve as a simple example of using the SIP.js API. * @public */ class SimpleUserPlus { /** * Constructs a new instance of the `SimpleUser` class. * @param server - SIP WebSocket Server URL. * @param options - Options bucket. See {@link SimpleUserOptions} for details. */ constructor(server, options = {}) { /** Delegate. */ Object.defineProperty(this, "delegate", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "options", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "session", { enumerable: true, configurable: true, writable: true, value: undefined }); Object.defineProperty(this, "sessionManager", { enumerable: true, configurable: true, writable: true, value: void 0 }); // Delegate this.delegate = options.delegate; // Copy options this.options = Object.assign({}, options); // Session manager options const sessionManagerOptions = { aor: this.options.aor, delegate: { onCallAnswered: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallAnswered) === null || _b === void 0 ? void 0 : _b.call(_a); }, onCallCreated: session => { var _a, _b, _c, _d; this.session = session; (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallBegin) === null || _b === void 0 ? void 0 : _b.call(_a, session); (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onCallCreated) === null || _d === void 0 ? void 0 : _d.call(_c); }, onCallReceived: session => { var _a, _b, _c, _d; (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallReceived) === null || _b === void 0 ? void 0 : _b.call(_a); (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onInvite) === null || _d === void 0 ? void 0 : _d.call(_c, session); }, onCallHangup: () => { var _a, _b; this.session = undefined; ((_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallHangup) && ((_b = this.delegate) === null || _b === void 0 ? void 0 : _b.onCallHangup()); }, onCallHold: (_s, held) => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallHold) === null || _b === void 0 ? void 0 : _b.call(_a, held); }, onCallDTMFReceived: (_s, tone, dur) => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallDTMFReceived) === null || _b === void 0 ? void 0 : _b.call(_a, tone, dur); }, onMessageReceived: message => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onMessageReceived) === null || _b === void 0 ? void 0 : _b.call(_a, message.request.body); }, onRegistered: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onRegistered) === null || _b === void 0 ? void 0 : _b.call(_a); }, onUnregistered: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onUnregistered) === null || _b === void 0 ? void 0 : _b.call(_a); }, onServerConnect: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onServerConnect) === null || _b === void 0 ? void 0 : _b.call(_a); }, onServerDisconnect: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onServerDisconnect) === null || _b === void 0 ? void 0 : _b.call(_a); }, onReconnectFailed: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectFailed) === null || _b === void 0 ? void 0 : _b.call(_a); }, onReconnectStart: () => { var _a, _b; return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectStart) === null || _b === void 0 ? void 0 : _b.call(_a); } }, maxSimultaneousSessions: 1, media: this.options.media, optionsPingInterval: this.options.optionsPingInterval, optionsPingRequestURI: this.options.aor, reconnectionAttempts: this.options.reconnectionAttempts, reconnectionDelay: this.options.reconnectionDelay, registererOptions: this.options.registererOptions, sendDTMFUsingSessionDescriptionHandler: this.options.sendDTMFUsingSessionDescriptionHandler, userAgentOptions: this.options.userAgentOptions }; this.sessionManager = new SessionManagerPlus(server, sessionManagerOptions); this.sessionManager.userAgent.stateChange.addListener(state => { var _a, _b; (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onUserAgentStateChange) === null || _b === void 0 ? void 0 : _b.call(_a, state); }); // Use the SIP.js logger this.logger = this.sessionManager.userAgent.getLogger('sip.SimpleUser'); } /** * Instance identifier. * @internal */ get id() { return this.options.userAgentOptions && this.options.userAgentOptions.displayName || 'Anonymous'; } /** The local media stream. Undefined if call not answered. */ get localMediaStream() { return this.session && this.sessionManager.getLocalMediaStream(this.session); } /** The remote media stream. Undefined if call not answered. */ get remoteMediaStream() { return this.session && this.sessionManager.getRemoteMediaStream(this.session); } /** * The local audio track, if available. * @deprecated Use localMediaStream and get track from the stream. */ get localAudioTrack() { return this.session && this.sessionManager.getLocalAudioTrack(this.session); } /** * The local video track, if available. * @deprecated Use localMediaStream and get track from the stream. */ get localVideoTrack() { return this.session && this.sessionManager.getLocalVideoTrack(this.session); } /** * The remote audio track, if available. * @deprecated Use remoteMediaStream and get track from the stream. */ get remoteAudioTrack() { return this.session && this.sessionManager.getRemoteAudioTrack(this.session); } /** * The remote video track, if available. * @deprecated Use remoteMediaStream and get track from the stream. */ get remoteVideoTrack() { return this.session && this.sessionManager.getRemoteVideoTrack(this.session); } /** * Connect. * @remarks * Start the UserAgent's WebSocket Transport. */ connect() { this.logger.log(`[${this.id}] Connecting UserAgent...`); return this.sessionManager.connect(); } /** * Disconnect. * @remarks * Stop the UserAgent's WebSocket Transport. */ disconnect() { this.logger.log(`[${this.id}] Disconnecting UserAgent...`); return this.sessionManager.disconnect(); } /** * Return true if connected. */ isConnected() { return this.sessionManager.isConnected(); } /** * Start receiving incoming calls. * @remarks * Send a REGISTER request for the UserAgent's AOR. * Resolves when the REGISTER request is sent, otherwise rejects. */ register(registererRegisterOptions) { this.logger.log(`[${this.id}] Registering UserAgent...`); return this.sessionManager.register(registererRegisterOptions); } /** * Stop receiving incoming calls. * @remarks * Send an un-REGISTER request for the UserAgent's AOR. * Resolves when the un-REGISTER request is sent, otherwise rejects. */ unregister(registererUnregisterOptions) { this.logger.log(`[${this.id}] Unregistering UserAgent...`); return this.sessionManager.unregister(registererUnregisterOptions); } /** * Make an outgoing call. * @remarks * Send an INVITE request to create a new Session. * Resolves when the INVITE request is sent, otherwise rejects. * Use `onCallAnswered` delegate method to determine if Session is established. * @param destination - The target destination to call. A SIP address to send the INVITE to. * @param inviterOptions - Optional options for Inviter constructor. * @param inviterInviteOptions - Optional options for Inviter.invite(). */ call(destination, inviterOptions, inviterInviteOptions) { this.logger.log(`[${this.id}] Beginning Session...`); if (this.session) { return Promise.reject(new Error('Session already exists.')); } return this.sessionManager.call(destination, inviterOptions, inviterInviteOptions).then(() => { return; }); } /** * Hangup a call. * @remarks * Send a BYE request, CANCEL request or reject response to end the current Session. * Resolves when the request/response is sent, otherwise rejects. * Use `onCallHangup` delegate method to determine if and when call is ended. */ hangup() { this.logger.log(`[${this.id}] Hangup...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.hangup(this.session).then(() => { this.session = undefined; }); } /** * Answer an incoming call. * @remarks * Accept an incoming INVITE request creating a new Session. * Resolves with the response is sent, otherwise rejects. * Use `onCallAnswered` delegate method to determine if and when call is established. * @param invitationAcceptOptions - Optional options for Inviter.accept(). */ answer(invitationAcceptOptions) { this.logger.log(`[${this.id}] Accepting Invitation...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.answer(this.session, invitationAcceptOptions); } /** * Decline an incoming call. * @remarks * Reject an incoming INVITE request. * Resolves with the response is sent, otherwise rejects. * Use `onCallHangup` delegate method to determine if and when call is ended. */ decline() { this.logger.log(`[${this.id}] rejecting Invitation...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.decline(this.session); } /** * Hold call * @remarks * Send a re-INVITE with new offer indicating "hold". * Resolves when the re-INVITE request is sent, otherwise rejects. * Use `onCallHold` delegate method to determine if request is accepted or rejected. * See: https://tools.ietf.org/html/rfc6337 */ hold() { this.logger.log(`[${this.id}] holding session...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.hold(this.session); } /** * Unhold call. * @remarks * Send a re-INVITE with new offer indicating "unhold". * Resolves when the re-INVITE request is sent, otherwise rejects. * Use `onCallHold` delegate method to determine if request is accepted or rejected. * See: https://tools.ietf.org/html/rfc6337 */ unhold() { this.logger.log(`[${this.id}] unholding session...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.unhold(this.session); } /** * Hold state. * @remarks * True if session is on hold. */ isHeld() { return this.session ? this.sessionManager.isHeld(this.session) : false; } /** * Mute call. * @remarks * Disable sender's media tracks. */ mute() { this.logger.log(`[${this.id}] disabling media tracks...`); return this.session && this.sessionManager.mute(this.session); } /** * Unmute call. * @remarks * Enable sender's media tracks. */ unmute() { this.logger.log(`[${this.id}] enabling media tracks...`); return this.session && this.sessionManager.unmute(this.session); } /** * Mute state. * @remarks * True if sender's media track is disabled. */ isMuted() { return this.session ? this.sessionManager.isMuted(this.session) : false; } /** * Send DTMF. * @remarks * Send an INFO request with content type application/dtmf-relay. * @param tone - Tone to send. */ sendDTMF(tone) { this.logger.log(`[${this.id}] sending DTMF...`); if (!this.session) { return Promise.reject(new Error('Session does not exist.')); } return this.sessionManager.sendDTMF(this.session, tone); } /** * Send a message. * @remarks * Send a MESSAGE request. * @param destination - The target destination for the message. A SIP address to send the MESSAGE to. */ message(destination, message) { this.logger.log(`[${this.id}] sending message...`); return this.sessionManager.message(destination, message); } } let has = Object.prototype.hasOwnProperty, prefix = '~'; /** * Constructor to create a storage for our `EE` objects. * An `Events` instance is a plain object whose properties are event names. * * @constructor * @private */ // eslint-disable-next-line @typescript-eslint/no-empty-function function Events() {} // // We try to not inherit from `Object.prototype`. In some engines creating an // instance in this way is faster than calling `Object.create(null)` directly. // If `Object.create(null)` is not supported we prefix the event names with a // character to make sure that the built-in object properties are not // overridden or used as an attack vector. // if (Object.create) { Events.prototype = Object.create(null); // // This hack is needed because the `__proto__` property is still inherited in // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5. // // eslint-disable-next-line no-const-assign if (!new Events().__proto__) prefix = false; } /** * Representation of a single event listener. * * @param {Function} fn The listener function. * @param {*} context The context to invoke the listener with. * @param {Boolean} [once=false] Specify if the listener is a one-time listener. * @constructor * @private */ function EE(fn, context, once) { this.fn = fn; this.context = context; this.once = once || false; } /** * Add a listener for a given event. * * @param {EventEmitter} emitter Reference to the `EventEmitter` instance. * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} context The context to invoke the listener with. * @param {Boolean} once Specify if the listener is a one-time listener. * @returns {EventEmitter} * @private */ function addListener(emitter, event, fn, context, once) { if (typeof fn !== 'function') { throw new TypeError('The listener must be a function'); } const listener = new EE(fn, context || emitter, once), evt = prefix ? prefix + event : event; if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);else emitter._events[evt] = [emitter._events[evt], listener]; return emitter; } /** * Clear event by name. * * @param {EventEmitter} emitter Reference to the `EventEmitter` instance. * @param {(String|Symbol)} evt The Event name. * @private */ function clearEvent(emitter, evt) { if (--emitter._eventsCount === 0) emitter._events = new Events();else delete emitter._events[evt]; } /** * Minimal `EventEmitter` interface that is molded against the Node.js * `EventEmitter` interface. * * @constructor * @public */ function EventEmitter() { this._events = new Events(); this._eventsCount = 0; } /** * Return an array listing the events for which the emitter has registered * listeners. * * @returns {Array} * @public */ EventEmitter.prototype.eventNames = function eventNames() { let names = [], events, name; if (this._eventsCount === 0) return names; for (name in events = this._events) { if (has.call(events, name)) names.push(prefix ? name.slice(1) : name); } if (Object.getOwnPropertySymbols) { return names.concat(Object.getOwnPropertySymbols(events)); } return names; }; /** * Return the listeners registered for a given event. * * @param {(String|Symbol)} event The event name. * @returns {Array} The registered listeners. * @public */ EventEmitter.prototype.listeners = function listeners(event) { const evt = prefix ? prefix + event : event, handlers = this._events[evt]; if (!handlers) return []; if (handlers.fn) return [handlers.fn]; for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) { ee[i] = handlers[i].fn; } return ee; }; /** * Return the number of listeners listening to a given event. * * @param {(String|Symbol)} event The event name. * @returns {Number} The number of listeners. * @public */ EventEmitter.prototype.listenerCount = function listenerCount(event) { const evt = prefix ? prefix + event : event, listeners = this._events[evt]; if (!listeners) return 0; if (listeners.fn) return 1; return listeners.length; }; /** * Calls each of the listeners registered for a given event. * * @param {(String|Symbol)} event The event name. * @returns {Boolean} `true` if the event had listeners, else `false`. * @public */ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) { const evt = prefix ? prefix + event : event; if (!this._events[evt]) return false; let listeners = this._events[evt], len = arguments.length, args, i; if (listeners.fn) { if (listeners.once) this.removeListener(event, listeners.fn, undefined, true); switch (len) { case 1: return listeners.fn.call(listeners.context), true; case 2: return listeners.fn.call(listeners.context, a1), true; case 3: return listeners.fn.call(listeners.context, a1, a2), true; case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true; case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true; case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true; } for (i = 1, args = new Array(len - 1); i < len; i++) { args[i - 1] = arguments[i]; } listeners.fn.apply(listeners.context, args); } else { let length = listeners.length, j; for (i = 0; i < length; i++) { if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true); switch (len) { case 1: listeners[i].fn.call(listeners[i].context); break; case 2: listeners[i].fn.call(listeners[i].context, a1); break; case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break; case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break; default: if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) { args[j - 1] = arguments[j]; } listeners[i].fn.apply(listeners[i].context, args); } } } return true; }; /** * Add a listener for a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} [context=this] The context to invoke the listener with. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.on = function on(event, fn, context) { return addListener(this, event, fn, context, false); }; /** * Add a one-time listener for a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn The listener function. * @param {*} [context=this] The context to invoke the listener with. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.once = function once(event, fn, context) { return addListener(this, event, fn, context, true); }; /** * Remove the listeners of a given event. * * @param {(String|Symbol)} event The event name. * @param {Function} fn Only remove the listeners that match this function. * @param {*} context Only remove the listeners that have this context. * @param {Boolean} once Only remove one-time listeners. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) { const evt = prefix ? prefix + event : event; if (!this._events[evt]) return this; if (!fn) { clearEvent(this, evt); return this; } const listeners = this._events[evt]; if (listeners.fn) { if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) { clearEvent(this, evt); } } else { for (var i = 0, events = [], length = listeners.length; i < length; i++) { if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) { events.push(listeners[i]); } } // // Reset the array, or remove it completely if we have no more listeners. // if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;else clearEvent(this, evt); } return this; }; /** * Remove all listeners, or those of the specified event. * * @param {(String|Symbol)} [event] The event name. * @returns {EventEmitter} `this`. * @public */ EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) { let evt; if (event) { evt = prefix ? prefix + event : event; if (this._events[evt]) clearEvent(this, evt); } else { this._events = new Events(); this._eventsCount = 0; } return this; }; // // Alias methods names because people roll like that. // EventEmitter.prototype.off = EventEmitter.prototype.removeListener; EventEmitter.prototype.addListener = EventEmitter.prototype.on; // // Expose the prefix. // EventEmitter.prefixed = prefix; // // Allow `EventEmitter` to be imported as module namespace. // EventEmitter.EventEmitter = EventEmitter; // // Expose the module. // if ('undefined' !== typeof module) { module.exports = EventEmitter; } /** 将小驼峰式字符串转换为小写下划线格式 * @param {string} * @returns {string} */ function upperCamelToLowerSnake(s) { s = s.replace(/\s/g, ''); s = s.replace(/([A-Z])/g, '_$1'); s = s.toLowerCase(); s = s.replace(/^_/, ''); s = s.replace(/_$/, ''); return s; } /** * 获取时间戳字符串 * @returns {string} 年-月-日 时:分:秒.毫秒 */ const getDateTime = () => { const timestamp = Date.now(); const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); const milliseconds = String(date.getMilliseconds()).padStart(3, '0'); const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`; return formattedDateTime; }; /** @enum LoggerLevels 日志输出等级 */ var LoggerLevels; (function (LoggerLevels) { LoggerLevels[LoggerLevels["error"] = 0] = "error"; LoggerLevels[LoggerLevels["warn"] = 1] = "warn"; LoggerLevels[LoggerLevels["log"] = 2] = "log"; LoggerLevels[LoggerLevels["debug"] = 3] = "debug"; })(LoggerLevels || (LoggerLevels = {})); /** @class Logger 通用的日志模块 */ class Logger { constructor(level, category, label) { Object.defineProperty(this, "_level", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "category", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "label", { enumerable: true, configurable: true, writable: true, value: void 0 }); this._level = level; this.category = category; this.label = label; } error(content) { this.genericLog(LoggerLevels.error, content); } warn(content) { this.genericLog(LoggerLevels.warn, content); } log(content) { this.genericLog(LoggerLevels.log, content); } debug(content) { this.genericLog(LoggerLevels.debug, content); } genericLog(levelToLog, content) { if (this._level >= levelToLog) { this.print(levelToLog, this.category, this.label, content); } } print(levelToLog, category, label, content) { if (typeof content === 'string') { const prefix = [getDateTime()]; if (label) { prefix.push(label); } content = prefix.concat(content).join(' | '); } switch (levelToLog) { case LoggerLevels.error: console.error(`%c${category}`, 'color: blue;', content); break; case LoggerLevels.warn: console.warn(`%c${category}`, 'color: blue;', content); break; case LoggerLevels.log: console.log(`%c${category}`, 'color: blue;', content); break; case LoggerLevels.debug: console.debug(`%c${category}`, 'color: blue;', content); break; } } get level() { return this._level; } set level(newLevel) { if (newLevel >= 0 && newLevel <= 3) { this._level = newLevel; } else if (newLevel > 3) { this._level = 3; // eslint-disable-next-line no-prototype-builtins } else if (LoggerLevels.hasOwnProperty(newLevel)) { this._level = newLevel; } else { this.error("invalid 'level' parameter value: " + JSON.stringify(newLevel)); } } } /** * @enum {string} CTI 初始化场景 * Manual: 手动外呼 * Robot: 机器人外呼 * Monitor: 监听 * Predictive: 预测式外呼 */ var Scene; (function (Scene) { Scene["Manual"] = "manual"; Scene["Robot"] = "robot"; Scene["Monitor"] = "monitor"; Scene["Predictive"] = "predictive"; Scene["Wechat"] = "wechat"; })(Scene || (Scene = {})); /** * @enum {string} CTI 监听场景 * All: 不区分场景 * Manual: 手动外呼 * Robot: 机器人外呼 * Predictive: 预测式外呼 */ var MonitorScene; (function (MonitorScene) { MonitorScene["All"] = "all"; MonitorScene["Manual"] = "manual"; MonitorScene["Robot"] = "robot"; MonitorScene["Predictive"] = "predictive"; })(MonitorScene || (MonitorScene = {})); /** * @interface {} 初始化 SD_CTI 需要的参数 * loggerLevel: 日志等级 * password: 临时的鉴权字符串,由业务方写死传进来 * scene: CTI 初始化场景 * monitorScene: 监听场景 * env: 环境变量 */ /** * @enum {string} Socket 状态 * Initial: 初始状态 * Connecting: Socket 开始建立连接 * Connected: Socket 建立连接成功 * Ready: 向 IM 发送第一个上行 login 消息收到成功回调 * Terminated: Socket 连接断开、各种 Socket 错误流转到本状态 */ var SocketStatus; (function (SocketStatus) { SocketStatus["Initial"] = "Initial"; SocketStatus["Connecting"] = "Connecting"; SocketStatus["Connected"] = "Connected"; SocketStatus["Ready"] = "Ready"; SocketStatus["ReTry"] = "ReTry"; SocketStatus["Terminated"] = "Terminated"; })(SocketStatus || (SocketStatus = {})); /** * @enum {string} SIP 状态 * Initial: 初始状态 * Started: SIP 的 User Agent 创建成功 * Connecting: SIP 底层 Socket 传输 TransportState.Connecting * Connected: SIP 底层 Socket 传输 TransportState.Connected * Ready: SIP Registerer 监听注册状态 RegistererState.Registered * Terminated: SIP Socket 断开、注册失败等各种错误、主动断开连接流转到本状态 */ var SIPStatus; (function (SIPStatus) { SIPStatus["Initial"] = "Initial"; SIPStatus["Started"] = "Started"; SIPStatus["Connecting"] = "Connecting"; SIPStatus["Connected"] = "Connected"; SIPStatus["Ready"] = "Ready"; SIPStatus["ReTry"] = "ReTry"; SIPStatus["Terminated"] = "Terminated"; })(SIPStatus || (SIPStatus = {})); /** * @enum {string} CTI 状态 * Initial: 初始状态 * Ready: SocketStatus Ready && SIPStatus Ready * Terminated: SocketStatus Terminated || SIPStatus Terminated || 正常调用 unInit 方法卸载 */ var CTIStatus; (function (CTIStatus) { CTIStatus["Initial"] = "Initial"; CTIStatus["Ready"] = "Ready"; CTIStatus["ReTry"] = "ReTry"; CTIStatus["Terminated"] = "Terminated"; })(CTIStatus || (CTIStatus = {})); /** * @enum {string} 通话状态 * 为了防止人工外呼方法被二次调用引发预期以外的问题,增加此状态的流转 * Started: 外呼已开始,此状态下不允许再次发起外呼 * Stopped: 外呼已结束,此状态下可以再次发起外呼 */ var CallStatus; (function (CallStatus) { CallStatus["Started"] = "Started"; CallStatus["Stopped"] = "Stopped"; })(CallStatus || (CallStatus = {})); /** * @enum {string} Session 状态 * CTI 目前只有 Invitation(接受会话)的场景,Inviter (主动发起会话)暂时没有 */ var SessionStatus; (function (SessionStatus) { /** * If `Inviter`, INVITE not sent yet. * If `Invitation`, SDK 收到 INVITE 通话请求,但尚未处理. */ SessionStatus["Initial"] = "Initial"; /** * If `Inviter`, sent INVITE and waiting for a final response. * If `Invitation`, received INVITE and attempting to send 200 final response (but has not sent it yet). */ SessionStatus["Establishing"] = "Establishing"; /** * If `Inviter`, sent INVITE and received 200 final response and sent ACK. * If `Invitation`, SDK 完成接受 INVITE 并发送 200 OK 确认接起,同时接通本地语音流. */ SessionStatus["Established"] = "Established"; /** * If `Inviter`, sent INVITE, sent CANCEL and now waiting for 487 final response to ACK (or 200 to ACK & BYE). * If `Invitation`, received INVITE, sent 200 final response and now waiting on ACK and upon receipt will attempt BYE * (as the protocol specification requires, before sending a BYE we must receive the ACK - so we are waiting). */ SessionStatus["Terminating"] = "Terminating"; /** * If `Inviter`, sent INVITE and received non-200 final response (or sent/received BYE after receiving 200). * If `Invitation`, SDK 收到 BYE 信令,发送 200 OK 挂断确认,会话结束. */ SessionStatus["Terminated"] = "Terminated"; })(SessionStatus || (SessionStatus = {})); /** * @enum {string} CTI 所有错误的分类 * SdkTerminated: SDK 不可用错误,需要重新初始化 * SdkError: SDK 状态可用,其他普通错误 * ServerTerminated: 服务端不可用错误,需要重新初始化,透传服务端 code,msg * ServerError: 服务端可用,普通错误,透传服务端 code,msg */ var CTIErrorType; (function (CTIErrorType) { CTIErrorType["SdkTerminated"] = "SdkTerminated"; CTIErrorType["SdkError"] = "SdkError"; CTIErrorType["ServerTerminated"] = "ServerTerminated"; CTIErrorType["ServerError"] = "ServerError"; })(CTIErrorType || (CTIErrorType = {})); /** * @enum {string} SdkTerminated 类型错误的 code 枚举 * CTITerminated: SDK 状态不可用,CTIStatus 的状态为 Terminated * GetUserMedia: 获取坐席媒体权限失败 * GetInitConfig: 调接口获取 CTI 初始化配置失败 * SocketOnError: 监听 socket.io 的 error 事件 * SocketOnConnectError: 监听 socket.io 的 connect_error 事件 * SocketOnDisconnect: 监听 socket.io 的 disconnect 事件 * SocketRepeatLogin: 多页面重复登录,IM 互踢事件 * SIPInitUserAgent: SIP UserAgent 初始化时启动失败 * SIPInitRegister: SIP Register 初始化时注册失败 * SIPUserAgentStateStopped: 监听 SIP UserAgent stateChange 事件状态变更为 Stopped * SIPTransportStateDisconnected: 监听 SIP Transport StateChange 事件状态变更为 Disconnect * SIPRegistererStateTerminated: 监听 SIP Registerer StateChange 事件状态变更为 Terminated * SIPOnDisconnect: 监听 SIP OnDisconnect 事件收到异常退出 error * SIPInitTransport: SIP Transport 初始化时连接失败 * SipHeartBeatErr: SIP发送心跳OPTIONS事件时收到异常结果 * SIPUnRegistered: 注册SIP时失败 * SocketOnReconnectFailed: Socket重连超过阈值且依然重连失败 */ var HskTerminatedCode; (function (HskTerminatedCode) { HskTerminatedCode["CTITerminated"] = "100001"; HskTerminatedCode["GetUserMedia"] = "100002"; HskTerminatedCode["GetInitConfig"] = "100003"; HskTerminatedCode["SocketOnError"] = "110001"; HskTerminatedCode["SocketOnConnectError"] = "110002"; HskTerminatedCode["SocketOnDisconnect"] = "110003"; HskTerminatedCode["SocketRepeatLogin"] = "110004"; HskTerminatedCode["SocketOnReconnectFailed"] = "110007"; HskTerminatedCode["SIPInitUserAgent"] = "120001"; HskTerminatedCode["SIPInitRegister"] = "120002"; HskTerminatedCode["SIPUserAgentStateStopped"] = "120003"; HskTerminatedCode["SIPTransportStateDisconnected"] = "120004"; HskTerminatedCode["SIPRegistererStateTerminated"] = "120005"; HskTerminatedCode["SIPOnDisconnect"] = "120006"; HskTerminatedCode["SIPInitTransport"] = "120007"; HskTerminatedCode["SipHeartBeatErr"] = "120008"; HskTerminatedCode["SIPUnRegistered"] = "120009"; })(HskTerminatedCode || (HskTerminatedCode = {})); /** * @enum {string} SdkError 类型错误的 code 枚举 * Answer: SIP accept 接起失败 * Bye: SIP bye 挂断失败 * InvitationCancel: SIP Invitation 会话请求被取消 * AssignStream: 播放语音流失败 * FetchError: 修饰器handleApiRes当进入到catch时上报此code */ var SdkErrorCode; (function (SdkErrorCode) { SdkErrorCode["Answer"] = "200001"; SdkErrorCode["Bye"] = "200002"; SdkErrorCode["InvitationCancel"] = "200003"; SdkErrorCode["AssignStream"] = "200004"; SdkErrorCode["FetchError"] = "200005"; })(SdkErrorCode || (SdkErrorCode = {})); /** * @enum {string} CTI 事件推送 * OnCtiError: CTI 错误事件,含前后端所有错误,SDK 推送 * OnSessionStatusChange: 坐席侧 SIP 会话状态变更事件,SDK 推送 * OnInitalSuccess: CTI 初始化成功事件,SDK 推送 * OnAgentWorkReport: 坐席&用户状态变更事件,Server 推送 * OnRingStart: 手动外呼用户未接听时,开始播放回铃音,Server 推送 * OnRingEnd: 手动外呼用户未接听时,播放回铃音结束,Server 推送 * OnAgentReport: 坐席状态变更事件,Server 推送 * OnCallReportInfo: 通话时长及通话次数等信息,Server 推送 * OnDetectedTone: 服务端收到音频信号后推送 */ var CTIEvent; (function (CTIEvent) { CTIEvent["OnCtiError"] = "OnCtiError"; CTIEvent["OnSessionStatusChange"] = "OnSessionStatusChange"; CTIEvent["OnInitalSuccess"] = "OnInitalSuccess"; CTIEvent["OnAgentWorkReport"] = "OnAgentWorkReport"; CTIEvent["OnRingStart"] = "OnRingStart"; CTIEvent["OnRingEnd"] = "OnRingEnd"; CTIEvent["OnDetectedTone"] = "OnDetectedTone"; CTIEvent["OnAgentReport"] = "OnAgentReport"; CTIEvent["OnCallReportInfo"] = "OnCallReportInfo"; // TODO: 后 7 个事件服务端未来不再推送时删掉 CTIEvent["OnCallRing"] = "OnCallRing"; CTIEvent["OnCallEnd"] = "OnCallEnd"; CTIEvent["OnCallAnswer"] = "OnCallAnswer"; CTIEvent["OnAgentGroupQuery"] = "OnAgentGroupQuery"; CTIEvent["OnMethodResponseEvent"] = "OnMethodResponseEvent"; CTIEvent["OnEventPrompt"] = "OnEventPrompt"; CTIEvent["OnPrompt"] = "OnPrompt"; })(CTIEvent || (CTIEvent = {})); var BaseOption; (function (BaseOption) { BaseOption["TrackParams"] = "trackParams"; BaseOption["ENV"] = "env"; BaseOption["LoggerLevel"] = "loggerLevel"; })(BaseOption || (BaseOption = {})); const items = [[BaseOption.TrackParams, {}], [BaseOption.ENV, 'test'], [BaseOption.LoggerLevel, LoggerLevels.debug]]; const baseOption = new Map(); const resetBaseOption = () => { items.forEach((i, v) => baseOption.set(i, v)); }; resetBaseOption(); const setBaseOption = (key, value, isInit = false) => { if (!isInit && typeof value === 'object') { baseOption.set(key, Object.assign(Object.assign({}, baseOption.get(key)), value)); } else { baseOption.set(key, value); } }; const getBaseOption = key => { return baseOption.get(key); }; const generateUUID = () => { return 'xxxxxxxxxxxx4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = Math.random() * 16 | 0; const v = c === 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }).replace(/-/g, ''); }; const getClientId = () => { if (!window.HS_CTI_CLIENT_ID) { window.HS_CTI_CLIENT_ID = generateUUID(); } return window.HS_CTI_CLIENT_ID; }; /** * @enum {string} 本地提示音枚举 * RingAudio: 来电振铃提示音 * WaitAudio: 主动外呼等待音 * ByeAudio: 挂断提示音 */ var AudioName; (function (AudioName) { AudioName["RingAudio"] = "_ringAudio"; AudioName["WaitAudio"] = "_waitAudio"; AudioName["ByeAudio"] = "_byeAudio"; })(AudioName || (AudioName = {})); /** * @enum {string} 埋点上报的分类 * FeSocket: Socket 类暴露的事件 * FeSIP: SIP 相关事件 * FeMethod: 业务调用 SDK 暴露的方法 * FeAPI: SDK 调用后端的接口返回值 * FeAPIError: SDK 调用后端的接口错误返回值 * FeIMDown: 后端通过 IM 返回的常规事件 * FeIMCmd: IM 返回的断开指令 * FeMedia: SDK 媒体事件 * FeStatu: SDK 是否状态流转事件 * FeEmit: SDK 抛出的事件 */ var TrackSource; (function (TrackSource) { TrackSource["FeSocket"] = "fe-socket"; TrackSource["FeSIP"] = "fe-sip"; TrackSource["FeMethod"] = "fe-method"; TrackSource["FeAPI"] = "fe-api"; TrackSource["FeAPIError"] = "fe-api-error"; TrackSource["FeIMDown"] = "fe-im-down"; TrackSource["FeIMCmd"] = "fe-im-cmd"; TrackSource["FeMedia"] = "fe-media"; TrackSource["FeStatus"] = "fe-status"; TrackSource["FeEmit"] = "fe-emit"; })(TrackSource || (TrackSource = {})); var SocketEvent; (function (SocketEvent) { SocketEvent["SetSocketStatus"] = "SetSocketStatus"; SocketEvent["SocketDownEvent"] = "SocketDownEvent"; })(SocketEvent || (SocketEvent = {})); var ExceptMessage; (function (ExceptMessage) { ExceptMessage["CommonNetworkErrorMsg"] = "\u5BF9\u4E0D\u8D77\uFF0C\u7F51\u7EDC\u72B6\u51B5\u6682\u65F6\u4E0D\u4F73\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\u3002"; ExceptMessage["CustomNetworkErrorMsg"] = "\u5BF9\u4E0D\u8D77\uFF0C\u8FDE\u63A5\u5931\u8D25\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\u3002\u5982\u679C\u95EE\u9898\u6301\u7EED\u51FA\u73B0\uFF0C\u8BF7\u8054\u7CFB\u6211\u4EEC\u7684\u6280\u672F\u4EBA\u5458\u3002\u611F\u8C22\u60A8\u7684\u7406\u89E3\u548C\u652F\u6301\u3002"; ExceptMessage["ManualCallAnswerErrorMsg"] = "\u5F53\u524D\u7535\u8BDD\u672A\u63A5\u901A\uFF0C\u901A\u8BDD\u5DF2\u7ED3\u675F\uFF0C\u8BF7\u91CD\u8BD5\u3002"; ExceptMessage["RobotOrWeChatAnswerErrorMsg"] = "\u5BF9\u4E0D\u8D77\uFF0C\u7531\u4E8E\u7528\u6237\u6302\u673A\u7B49\u539F\u56E0\uFF0C\u5F53\u524D\u7535\u8BDD\u672A\u63A5\u901A\uFF0C\u8BF7\u7B49\u5F85\u4E0B\u4E00\u901A\u7535\u8BDD\u3002"; ExceptMessage["SipByeErrorMsg"] = "\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u6302\u65AD\u3002"; ExceptMessage["CTIRepeatLoginMsg"] = "\u5F53\u524D\u5750\u5E2D\u5DF2\u88AB\u5176\u4ED6\u7EC8\u7AEF\u66FF\u4EE3\u3002"; })(ExceptMessage || (ExceptMessage = {})); const methodExceptMsgMap = { checkIn: ExceptMessage.CommonNetworkErrorMsg, checkOut: ExceptMessage.CommonNetworkErrorMsg, setIdle: ExceptMessage.CustomNetworkErrorMsg, setBusy: ExceptMessage.CommonNetworkErrorMsg, makeCall: ExceptMessage.CommonNetworkErrorMsg, answer: ExceptMessage.CommonNetworkErrorMsg, bye: ExceptMessage.CommonNetworkErrorMsg, loadAgentGroupData: ExceptMessage.CommonNetworkErrorMsg, listen: ExceptMessage.CommonNetworkErrorMsg, setActiveService: ExceptMessage.CustomNetworkErrorMsg }; const baseRequireParams = ['agent_id', 'saas_id', // 'password', 'env', 'scene']; const monitorRequireParams = ['monitorScene']; const allRequiredParams = [...baseRequireParams, ...monitorRequireParams]; class HsSocket extends EventEmitter { constructor(socketOptions) { super(); Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "socket", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 初始化 socket 需要的参数 */ Object.defineProperty(this, "socketOptions", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 心跳延迟时间 */ Object.defineProperty(this, "heartBeatDelay", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 主动关闭链接时间 */ Object.defineProperty(this, "closeHeartBeatDelay", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 心跳检测定时器 */ Object.defineProperty(this, "heartBeatTimer", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 清空心跳检测定时器 */ Object.defineProperty(this, "closeHeartBeatTimer", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 超时次数 */ // private timeOutCount: number /** 最大超时次数限制 */ Object.defineProperty(this, "imRetryCount", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 本次 socket 会话唯一 id */ Object.defineProperty(this, "sessionId", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 页面关闭时 关闭 socket */ window.addEventListener('onunload', this.closeSocket); this.logger = new Logger(socketOptions.loggerLevel, 'HsSocket'); this.socket = undefined; this.heartBeatTimer = null; this.closeHeartBeatTimer = null; // this.timeOutCount = 0 this.sessionId = ''; this.socketOptions = socketOptions; this.heartBeatDelay = socketOptions.imHeartTime * 1000 || 3000; this.closeHeartBeatDelay = socketOptions.imHeartTime * 1000 || 3000; this.imRetryCount = socketOptions.imRetryCount || 10; } /** @public initSocket 初始化 Socket 连接 */ initSocket() { /** 如果有未断开的连接先断开 */ this.closeSocket(); // 设置状态为连接中 this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Connecting }); /** https://socket.io/docs/v2/client-api/#iourl */ this.socket = io(this.socketOptions.imWsServer, { transports: ['websocket'], reconnectionAttempts: this.imRetryCount, reconnectionDelay: this.heartBeatDelay, reconnectionDelayMax: this.heartBeatDelay + 1000, timeout: this.heartBeatDelay, reconnection: true }); /** https://socket.io/docs/v2/client-api/#event-error */ this.socket.on('error', error => { this.logger.error(`socket_error | ${JSON.stringify(error)}`); }); /** https://socket.io/docs/v2/client-api/#event-connect_error-1 */ this.socket.on('connect_error', error => { const errorData = `socket_connect_error | ${JSON.stringify(error)}`; this.logger.warn(errorData); }); this.socket.on('reconnecting', res => { this.logger.error(`socket_warn | socket_reconnecting | ${res}`); this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.ReTry }); }); this.socket.on('reconnect', res => { this.logger.error(`socket_warn | socket_reconnect | ${res}`); this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Ready }); }); this.socket.on('reconnect_failed', error => { this.logger.error(`socket_warn | socket_reconnect_failed | ${error}`); this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Terminated, code: HskTerminatedCode.SocketOnReconnectFailed, error: `${error}` }); }); /** https://socket.io/docs/v2/client-api/#event-connect */ this.socket.on('connect', () => { this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Connected }); this.socketLogin(); }); /** https://socket.io/docs/v2/client-api/#event-disconnect */ this.socket.on('disconnect', reason => { const errorMessage = `socket_disconnect | ${reason}`; this.logger.warn(errorMessage); }); /** 服务端下行事件 */ this.socket.on('common_down_data', e => { console.log(e, 3434343434); if (e && JSON.parse(e) && JSON.parse(e).data) { this.emit(SocketEvent.SocketDownEvent, { eventData: JSON.parse(JSON.parse(e).data) }); } }); /** 服务端下行指令 */ this.socket.on('common_down_cmd', e => { const { clientSessionId } = JSON.parse(e); console.log('dsdsdsdsdsds', clientSessionId); if (clientSessionId === this.sessionId) { this.logger.error(`socket status | ${SocketStatus.Terminated} | 坐席在其他页面重新初始化,本页面被踢出`); this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Terminated, code: HskTerminatedCode.SocketRepeatLogin, error: '您已在其他页面签入,当前页面连接已断开' }); } }); } /** @private socketLogin 客户端上行登录事件 */ socketLogin() { const data = { // appCode: this.socketOptions.appCode || '1111', // token: this.socketOptions.token || '1111', userId: this.socketOptions.agent_id }; this.socket && this.socket.emit('login', data, sessionId => { console.log(sessionId, '测试一下'); this.emit(SocketEvent.SetSocketStatus, { status: SocketStatus.Ready }); setBaseOption(BaseOption.TrackParams, { socket_session_id: sessionId }); this.sessionId = sessionId; this.startHeartbeat(); }); } /** @public closeSocket 关闭 socket 连接 */ closeSocket() { if (this.socket) { this.socket.io.opts.reconnection = false; this.socket.close(); // 清除之前的监听事件 this.socket.removeAllListeners(); } this.socket = undefined; this.sessionId = ''; if (this.heartBeatTimer) { window.clearTimeout(this.heartBeatTimer); this.heartBeatTimer = null; } if (this.closeHeartBeatTimer) { window.clearTimeout(this.closeHeartBeatTimer); this.closeHeartBeatTimer = null; } } /** @private startHeartbeat 开启心跳检测 */ startHeartbeat() { if (this.heartBeatTimer) { window.clearTimeout(this.heartBeatTimer); this.heartBeatTimer = null; } if (this.closeHeartBeatTimer) { window.clearTimeout(this.closeHeartBeatTimer); this.closeHeartBeatTimer = null; } this.socket && this.heartbeatEvent(); } /** @private heartbeatEvent websocket心跳检测 */ heartbeatEvent() { this.heartBeatTimer = setTimeout(() => { this.socket && this.sendHeartbeat(); /** 如果心跳检测一直没回应,则进行重连 */ this.closeHeartBeatTimer = setTimeout(() => { this.logger.warn('socket_heart_beat | 心跳超时,即将重新连接'); this.initSocket(); }, this.closeHeartBeatDelay); }, this.heartBeatDelay); } /** @private sendHeartbeat 客户端上行心跳事件 */ sendHeartbeat() { this.socket && this.socket.emit('heartbeat', JSON.stringify(Object.assign({}, this.socketOptions)), () => { this.startHeartbeat(); }); } } const apiLogger = new Logger(window.ctiLoggerLevel || getBaseOption(BaseOption.LoggerLevel), 'HsApi'); const random16Hex = () => (0x10000 | Math.random() * 0x10000).toString(16).substr(1); const random64Hex = () => random16Hex() + random16Hex() + random16Hex() + random16Hex(); function JPOST({ baseUrl, url, data }) { return __awaiter(this, void 0, void 0, function* () { const id = random64Hex(); const response = yield fetch(baseUrl + url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-B3-TraceId': id, 'X-B3-SpanId': id }, body: JSON.stringify(data) }); if (!response.ok) { apiLogger.error(`api response | ${url} | Request failed with status ${response.status}`); } return response.json(); }); } const hsTrackJPOST = ({ baseUrl, url = '', data }) => { // eslint-disable-next-line no-async-promise-executor return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () { try { const res = yield JPOST({ baseUrl, url, data }); const { code, msg } = res; console.log(msg); if (code === 0) { apiLogger.log(`api response | ${url} | ${JSON.stringify(res)}`); } else { apiLogger.error(`api response | ${url} | ${JSON.stringify(res)}`); } resolve(res); } catch (e) { apiLogger.error(`api response | ${url} | ${JSON.stringify(e)}`); reject(e); } })); }; // 获取初始化配置 const baseUrl = 'http://192.168.100.159:8090'; const getInitConf = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/get-init-config', data }); }; // 坐席签入 const agentCheckIn = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/check-in', data }); }; // 坐席签出 const agentCheckOut = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/check-out', data }); }; // 坐席置闲 const agentSetIdle = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/idle', data }); }; // 坐席置忙 const agentSetBusy = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/busy', data }); }; // 获取坐席状态 const getAgentStatus = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/agent-state', data }); }; // 外呼 const manualCall = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/manual-call', data }); }; // 挂断 const manualHang = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/manual-hang', data }); }; // 发起监听 const listen = data => { return hsTrackJPOST({ baseUrl, url: '/open/agent/listen', data }); }; // 获取监控组成员信息 const loadAgentGroupData = data => { return hsTrackJPOST({ baseUrl, url: '/open/monitor/load-agent-group-data', data }); }; // 机器人外呼-签入人工组 const setActiveServiceTask = data => { return hsTrackJPOST({ baseUrl, url: '/open/human-service/member-active', data }); }; // 获取 cti 流程 ID const getCtiFlowId = data => { return hsTrackJPOST({ baseUrl, url: '/open/num/generate', data }); }; /** * @function getServerErrorType 根据服务端返回的 code 生成错误类型 * @param {number} code * @returns {CTIErrorType} */ function getServerErrorType(code) { if (code >= 300001 && code <= 399999) return CTIErrorType.ServerTerminated;else return CTIErrorType.ServerError; } /** @function getUserMedia 获取媒体权限 */ function getUserMedia() { // eslint-disable-next-line @typescript-eslint/no-explicit-any return function (_target, _methodName, descriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args) { if (navigator && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(() => { this.logger.debug('media | getUserMedia | 获取浏览器媒体权限成功'); return originalMethod.apply(this, args); }).catch(error => { this.logger.error(`media | getUserMedia | ${error}`); this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.SdkTerminated, code: HskTerminatedCode.GetUserMedia, msg: error.name === 'NotAllowedError' ? '用户拒绝了获取麦克风权限!' : '获取麦克风权限失败', method: 'getUserMedia' }, { source: TrackSource.FeMedia, event_name: 'get_user_media_error', error: error.name }); }); } else { this.logger.error('media | getUserMedia | 浏览器版本过低,不支持获取媒体权限'); this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.SdkTerminated, code: HskTerminatedCode.GetUserMedia, msg: '浏览器版本过低,不支持获取媒体权限', method: 'getUserMedia' }, { source: TrackSource.FeMedia, event_name: 'get_user_media_not_support' }); } }; return descriptor; }; } /** * @function checkCTIStatus 校验 cti 状态 * @param {string} msg */ function checkCTIStatus(msg) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return function (_target, methodName, descriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args) { /** 调用 SDK method */ this.logger.log(`sdk method | ${methodName} | ${msg}`); /** 校验 CTIStatus, 如果不是 Ready 状态,直接报错 */ if (this.getCTIStatus !== CTIStatus.Ready) { const errorData = { type: CTIErrorType.SdkTerminated, code: HskTerminatedCode.CTITerminated, msg: methodExceptMsgMap[methodName], method: methodName, terminated_source: this._terminatedStatusList }; this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData); return Promise.reject(errorData); } return originalMethod.apply(this, args); }; return descriptor; }; } /** @function handleApiRes 统一处理服务端接口返回值 */ function handleApiRes() { // eslint-disable-next-line @typescript-eslint/no-explicit-any return function (_target, methodName, descriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args) { return __awaiter(this, void 0, void 0, function* () { try { const res = yield originalMethod.apply(this, args); const { code, msg } = res; if (code !== 0) { const serverErrorType = getServerErrorType(code); const errorData = { type: serverErrorType, code, msg: msg, method: methodName }; this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData); if (serverErrorType === CTIErrorType.ServerTerminated) { this.setCTIStatus(CTIStatus.Terminated); } return Promise.reject(errorData); } else { return Promise.resolve(res); } } catch (error) { const errorData = { type: 'fetch_error', code: SdkErrorCode.FetchError, msg: JSON.stringify(error), method: methodName }; this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData); return Promise.reject(errorData); } }); }; return descriptor; }; } function validateParams() { // eslint-disable-next-line @typescript-eslint/no-explicit-any return function (_target, _methodName, descriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const currentParams = args[0]; let requireList = []; if (currentParams.scene === Scene.Monitor) { requireList = allRequiredParams; } else { requireList = baseRequireParams; } requireList.forEach(param => { if (!currentParams[param]) { throw `参数[${param}]为必填参数`; } }); return originalMethod.apply(this, args); }; return descriptor; }; } function generateUniqueId() { return 'id-' + Date.now() + '-' + Math.floor(Math.random() * 10000); } /** * 本地提示音 * _ringAudio 机器人外呼/监听,等待接起提示音 * _waitAudio 主动外呼,点击拨打时的等待音 * _byeAudio 结束通话提示音 */ const audioList = { _ringAudio: 'http://static.fuxicarbon.com/hs-cti/ring.wav', _waitAudio: 'http://static.fuxicarbon.com/hs-cti/manual.wav', _byeAudio: 'http://static.fuxicarbon.com/hs-cti/bye.wav' }; /** @class HsCTI 红杉外呼类 */ class HsCTI extends EventEmitter { constructor(hsCTIInitOptions) { const { saas_id, agent_id, scene, env, loggerLevel } = hsCTIInitOptions; super(); Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "loggerLevel", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "scene", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "agent_id", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "saas_id", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 接口返回的初始化配置 */ Object.defineProperty(this, "_initOptions", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** IM socket的实例 */ Object.defineProperty(this, "_socket", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** sip.js UA实例 */ Object.defineProperty(this, "_sipUserAgent", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_callId", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_ctiFlowIdList", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 基本参数 */ Object.defineProperty(this, "_baseParams", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 等待提示音播放器 */ Object.defineProperty(this, "_waitAudio", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 振铃提示音播放器 */ Object.defineProperty(this, "_ringAudio", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 结束通话提示音 */ Object.defineProperty(this, "_byeAudio", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** 远端音频流播放器 */ Object.defineProperty(this, "_remoteAudio", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** CTI状态 */ Object.defineProperty(this, "_ctiStatus", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_ctiStatusList", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** sip状态 */ Object.defineProperty(this, "_sipStatus", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_sipStatusList", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** socket状态 */ Object.defineProperty(this, "_socketStatus", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_socketStatusList", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_callStatus", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_terminatedStatusList", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.loggerLevel = window.ctiLoggerLevel || loggerLevel || LoggerLevels.log; this.logger = new Logger(this.loggerLevel, 'HsCTI'); this._waitAudio = new Audio(); this._ringAudio = new Audio(); this._byeAudio = new Audio(); this._remoteAudio = new Audio(); this.saas_id = saas_id; this.agent_id = agent_id; this.scene = scene; this._callId = ''; setBaseOption(BaseOption.ENV, env); setBaseOption(BaseOption.LoggerLevel, this.loggerLevel); this._baseParams = { agent_id, saas_id, scene }; const baseTrackParams = { agent_id: agent_id, vcc_id: saas_id, scene }; if (scene === Scene.Monitor) { const { monitorScene } = hsCTIInitOptions; this._baseParams = Object.assign(Object.assign({}, this._baseParams), { monitorScene }); setBaseOption(BaseOption.TrackParams, Object.assign(Object.assign({}, baseTrackParams), { monitor_scene: monitorScene }), true); } else { setBaseOption(BaseOption.TrackParams, baseTrackParams, true); } this.initInstanceOptions(); this.setCTIStatus(CTIStatus.Initial); // 首次获取实例时,将外呼状态置为未开始 this._callStatus = CallStatus.Stopped; this._terminatedStatusList = []; } get getCTIStatus() { return this._ctiStatus; } get getSocketStatus() { return this._socketStatus; } get getSIPStatus() { return this._sipStatus; } static getInstance(hsCTIInitOptions) { if (!HsCTI.instance) { HsCTI.instance = new HsCTI(hsCTIInitOptions); } setBaseOption(BaseOption.TrackParams, { clientId: getClientId() }); return HsCTI.instance; } /** @private 重置实例 */ initInstanceOptions() { this._ctiFlowIdList = []; this._ctiStatusList = []; this._sipStatusList = []; this._socketStatusList = []; HsCTI.instance = undefined; this._initOptions = undefined; resetBaseOption(); } /** @private 优雅关闭 SIP 和 socket */ clearSocketAndSip() { var _a, _b, _c; (_a = this._socket) === null || _a === void 0 ? void 0 : _a.closeSocket(); (_b = this._sipUserAgent) === null || _b === void 0 ? void 0 : _b.unregister(); (_c = this._sipUserAgent) === null || _c === void 0 ? void 0 : _c.disconnect(); this._sipUserAgent = undefined; } /** * @private 设置等待音 src * @param {AudioName} audioName * @param {boolean} loop */ setAudioSrc(audioName, loop) { this.logger.debug(`media | 设置等待音 src: ${audioList[audioName]}`); this[audioName].src = audioList[audioName]; this[audioName].currentTime = 0; this[audioName].autoplay = false; this[audioName].loop = loop; } playAudio(audioName) { this.logger.debug(`media | 播放音频:${audioName}`); this[audioName].play(); } stopLocalAudio() { switch (this.scene) { case Scene.Robot: case Scene.Monitor: this.stopAudio(AudioName.RingAudio, true); break; case Scene.Manual: this.stopAudio(AudioName.WaitAudio, true); break; } } stopAudio(audioName, loop) { this.logger.debug(`media | 停止音频播放:${audioName}`); this[audioName].src = ''; setTimeout(() => { this.setAudioSrc(audioName, loop); }, 1000); } init() { // 不允许重复初始化,必须在销毁sdk后才能重新初始化 const [lastStatus] = this._ctiStatusList.slice(-1); if (lastStatus !== CTIStatus.Initial) return; this.setAudioSrc(AudioName.ByeAudio, false); switch (this.scene) { case Scene.Robot: case Scene.Monitor: this.setAudioSrc(AudioName.RingAudio, true); break; case Scene.Manual: this.setAudioSrc(AudioName.WaitAudio, true); break; } this.getInitConfig(); } getInitConfig() { return __awaiter(this, void 0, void 0, function* () { const res = yield getInitConf(this._baseParams); const { code, data, msg } = res; let initOptions = data; /** * 由于后端接口层面对于查询来说,查不到不代表异常,所以即使查不到也是正向流程 * 因此如果传入一个不存在的agent_id,后端也会返回code=0,但是data=null * 这里需要根据code===0&&initOptions存在进行联合判断,只有两个条件都成立,才表示该坐席获取fs以及im等配置成功 */ initOptions = Object.assign(Object.assign({}, initOptions), { imHeartTime: 3, // IM 重试次数 imRetryCount: 3, // FS 心跳间隔 fsHeartTime: 60, // FS 重试次数, fsRetryCount: 3, // FS 重试间隔时间 fsRetryTime: 60, // FS 注册过期时间 fsRegisterExpireTime: 84000, // 单次初始化唯一 ID ctiSessionId: generateUniqueId(), // IM websocket url imWsServer: 'ws://192.168.100.159:8091/ws/cs-im', // IM websocket path imWsPath: 'ws/cs-im' }); if (code === 0 && initOptions) { setBaseOption(BaseOption.TrackParams, { sip_server: initOptions.sipServer, cti_session_id: initOptions.ctiSessionId }); this._initOptions = initOptions; this.initSocket(initOptions); this.initSip(initOptions); } else { this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.ServerTerminated, code: HskTerminatedCode.GetInitConfig, // 如果code===0并且initOptions不存在时,代表未查到对应坐席,此时接口返回的msg是空字符串,所以提示文案要特定处理 msg: code === 0 && !initOptions ? '未查到坐席' : msg, method: 'getInitConfig', res_code: code }); } }); } initSocket(initOptions) { this.setSocketStatus({ status: SocketStatus.Initial }); this._socket = new HsSocket({ agent_id: this.agent_id, saas_id: this.saas_id, // appCode: initOptions.appCode, imWsServer: initOptions.imWsServer, imWsPath: initOptions.imWsPath, imHeartTime: initOptions.imHeartTime, imRetryCount: initOptions.imRetryCount, ctiSessionId: initOptions.ctiSessionId, loggerLevel: this.loggerLevel }); this._socket.on(SocketEvent.SocketDownEvent, ({ eventData }) => { const { eventName, ext } = eventData; this.logger.log(`socket server down | ${eventName} | ${JSON.stringify(ext)}`); this.handleSocketDownEvent({ eventName, ext }); }); this._socket.on(SocketEvent.SetSocketStatus, res => { console.log(`set socket statussdsdsdsdsdsdds | ${JSON.stringify(res)}`); this.setSocketStatus(res); }); this._socket.initSocket(); } handleSocketDownEvent({ eventName, ext }) { const ctiFlowId = this._baseParams.ctiFlowId; const extCtiFlowId = ext.ctiFlowId; if ([Scene.Manual, Scene.Monitor].includes(this.scene) && ctiFlowId && extCtiFlowId && ctiFlowId !== extCtiFlowId) { this.logger.error(`cti_flow_id | 不一致! fe: ${ctiFlowId}, server: ${extCtiFlowId}, eventName: ${eventName}`); return; } this.serverEventEmit({ eventName, ext }); } /** @private serverEventEmit 统一处理服务端推送的事件 */ serverEventEmit({ eventName, ext }) { const NO_EMIT_EVENT_LIST = [CTIEvent.OnRingStart, CTIEvent.OnDetectedTone, CTIEvent.OnRingEnd]; switch (eventName) { case CTIEvent.OnRingStart: this.stopLocalAudio(); break; case CTIEvent.OnDetectedTone: this.stopLocalAudio(); break; case CTIEvent.OnAgentWorkReport: break; case CTIEvent.OnCallEnd: /** 通话结束:重置可拨打状态,清空本轮通话的CallId */ this._callStatus = CallStatus.Stopped; setBaseOption(BaseOption.TrackParams, { call_id: '' }); break; case CTIEvent.OnRingEnd: /** TODO: 后 4 个事件未来服务端不再推送时删掉 */ break; // do nothing } if (!NO_EMIT_EVENT_LIST.includes(eventName)) { this.eventEmitAndTrack(eventName, ext); } } initSip(initOptions) { this.setSipStatus({ status: SIPStatus.Initial }); const server = initOptions.wss_server; const simpleUserPlusOptions = { aor: initOptions.sip_server, reconnectionAttempts: initOptions.fsRetryCount, reconnectionDelay: initOptions.fsRetryTime, optionsPingInterval: initOptions.fsHeartTime, media: { remote: { audio: this._remoteAudio } }, userAgentOptions: { gracefulShutdown: true, authorizationPassword: initOptions.phone_pwd, sessionDescriptionHandlerFactoryOptions: { constraints: { audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true }, video: false }, peerConnectionConfiguration: { // iceServers: [] iceServers: [{ urls: initOptions.ice_server }] } } } }; simpleUserPlusOptions.delegate = this.sipDelegate(); this._sipUserAgent = new SimpleUserPlus(server, simpleUserPlusOptions); this._sipUserAgent.connect().catch(error => { const err = `SIP UserAgent 启动失败:${JSON.stringify(error)}`; this.logger.error(err); this.setSipStatus({ status: SIPStatus.Terminated, code: HskTerminatedCode.SIPInitUserAgent, error: err, method: 'initSIPJS' }); }); } sipDelegate() { return { onServerConnect: () => { this._sipUserAgent.register(); }, onServerDisconnect: () => { // 正向关闭UA时会进入此回调 this.setSipStatus({ status: SIPStatus.Terminated }); }, onReconnectStart: () => { // 开始重连时将状态流转为ReTry this.setSipStatus({ status: SIPStatus.ReTry }); }, onReconnectFailed: () => { // 重连失败时会进如此回调 this.setSipStatus({ status: SIPStatus.Terminated, code: HskTerminatedCode.SIPOnDisconnect, error: `超过重连次数,终止重连`, method: 'onReconnectFailed' }); this.setSipStatus({ status: SIPStatus.Terminated }); }, onRegistered: () => { this.setSipStatus({ status: SIPStatus.Ready }); }, onUserAgentStateChange: state => { switch (state) { case UserAgentState.Started: this.setSipStatus({ status: SIPStatus.Started }); break; case UserAgentState.Stopped: this.setSipStatus({ status: SIPStatus.Terminated, code: this._sipStatusList.includes(SIPStatus.Started) ? HskTerminatedCode.SIPUserAgentStateStopped : HskTerminatedCode.SIPInitUserAgent, error: 'SIP UserAgentState 状态停止', method: 'onUserAgentStateChange' }); break; } }, onInvite: invitation => { // this.scene = Scene.Robot const callId = invitation.request.getHeader('P-LIBRA-CallId') || ''; console.log(callId, 2888888888); // const ctiFlowId = // invitation.request.getHeader('P-LIBRA-CtiFlowId') || '' // if (ctiFlowId != this._baseParams.ctiFlowId) { // this.logger.error( // `cti_flow_id 不一致! fe: ${this._baseParams.ctiFlowId} | P-LIBRA-CtiFlowId: ${ctiFlowId}` // ) // return // } this._callId = callId; console.log(callId, 1888888888); setBaseOption(BaseOption.TrackParams, { call_id: callId }); this.sessionStateChangeAndTrack(invitation.state); if ([Scene.Robot, Scene.Monitor].includes(this.scene)) { this.playAudio(AudioName.RingAudio); console.log('playAudio', 1888888888); } if ([Scene.Manual].includes(this.scene)) { this.answer(); console.log('answer', 1888888888); } invitation.delegate = { onCancel: cancel => { const error = `sip_invitation_on_cancel | ${JSON.stringify(cancel)}`; this.logger.error(error); this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.SdkError, code: SdkErrorCode.InvitationCancel, msg: '当前通话已结束', method: 'sipDelegate' }, error); } }; invitation.stateChange.addListener(state => { this.sessionStateChangeAndTrack(state); switch (state) { case SessionState.Initial: case SessionState.Establishing: break; case SessionState.Established: if ([Scene.Robot, Scene.Monitor].includes(this.scene)) { this.stopLocalAudio(); } break; case SessionState.Terminating: break; case SessionState.Terminated: this.playAudio(AudioName.ByeAudio); setTimeout(() => { this.stopAudio(AudioName.ByeAudio, false); }, 1000); this.stopLocalAudio(); this._callStatus = CallStatus.Stopped; setBaseOption(BaseOption.TrackParams, { call_id: '' }); break; } }); } }; } sessionStateChangeAndTrack(status) { this.eventEmitAndTrack(CTIEvent.OnSessionStatusChange, { status }); const trackName = `sip_session_state_${upperCamelToLowerSnake(status)}`; this.logger.log(trackName); } /** * @private setCTIStatus CTI 状态流转 * @param {CTIStatus} ctiStatus */ setCTIStatus(ctiStatus) { if (ctiStatus === this.getCTIStatus) return; this._ctiStatus = ctiStatus; this._ctiStatusList.push(ctiStatus); const logInfo = `cti status | ${ctiStatus} | ${this._ctiStatusList}`; if (ctiStatus === CTIStatus.Terminated) { this.setSocketStatus({ status: SocketStatus.Terminated }); this.setSipStatus({ status: SIPStatus.Terminated }); this.stopLocalAudio(); this.logger.warn(logInfo); } else { this.logger.debug(logInfo); } if (ctiStatus === CTIStatus.Ready) { this.setSocketStatus({ status: SocketStatus.Ready }); this.setSipStatus({ status: SIPStatus.Ready }); this._terminatedStatusList = []; if (!this._ctiStatusList.includes(CTIStatus.ReTry)) { this.checkIn(); } } } /** * @private setSocketStatus Socket 状态流转 * @param SocketStatusChangeParams Socket 状态流转参数及错误详情等 */ setSocketStatus({ status, code, error }) { if (status === this.getSocketStatus) return; this._socketStatus = status; this._socketStatusList.push(status); const logInfo = `socket status | ${status} | ${this._socketStatusList}`; status === SocketStatus.Terminated ? this.logger.warn(logInfo) : this.logger.debug(logInfo); this.socketOrSipStatusChange(status, this.getSIPStatus); if (error) { this._terminatedStatusList.push(code); this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.SdkTerminated, code, msg: code === HskTerminatedCode.SocketRepeatLogin ? ExceptMessage.CTIRepeatLoginMsg : ExceptMessage.CommonNetworkErrorMsg, method: 'setSocketStatus' }, { error_msg: error, socket_status_list: this._socketStatusList, sip_status_list: this._sipStatusList, cti_status_list: this._ctiStatusList }); this.clearSocketAndSip(); } } /** * @private setSipStatus SIP 状态流转 * @param SIPStatusChangeParams SIP 状态流转参数及错误详情等 */ setSipStatus({ status, code, error, method }) { if (status === this.getSIPStatus) return; this._sipStatus = status; this._sipStatusList.push(status); const logInfo = `sip status | ${status} | ${this._sipStatusList}`; status === SIPStatus.Terminated ? this.logger.warn(logInfo) : this.logger.debug(logInfo); this.socketOrSipStatusChange(this.getSocketStatus, status); if (error) { /** 抛出 SIP 类型的错误详情 */ this._terminatedStatusList.push(code); this.eventEmitAndTrack(CTIEvent.OnCtiError, { type: CTIErrorType.SdkTerminated, code, msg: ExceptMessage.CommonNetworkErrorMsg, method: method || 'setSipStatus' }, { error_msg: error, socket_status_list: this._socketStatusList, sip_status_list: this._sipStatusList, cti_status_list: this._ctiStatusList }); this.clearSocketAndSip(); } } /** * @private socketOrSipStatusChange Socket 或 SIP 状态变化可能引起 CTI 状态变化 * @param {SocketStatus} socketStatus * @param {SIPStatus} sipStatus */ socketOrSipStatusChange(socketStatus, sipStatus) { if (socketStatus === SocketStatus.Ready && sipStatus === SIPStatus.Ready) { this.setCTIStatus(CTIStatus.Ready); } // if (socketStatus === SocketStatus.Ready) { // this.setCTIStatus(CTIStatus.Ready) // } if (socketStatus === SocketStatus.ReTry || sipStatus === SIPStatus.ReTry) { this.setCTIStatus(CTIStatus.ReTry); } if (socketStatus === SocketStatus.Terminated || sipStatus === SIPStatus.Terminated) { this.setCTIStatus(CTIStatus.Terminated); } } /** * @private eventEmitAndTrack SDK 对外抛出事件并统一埋点 * @param {CTIEvent} eventName 事件名称 * @param {object} ext 事件参数 * @param { error } error 错误详情或错误辅助信息 */ eventEmitAndTrack(eventName, ext, error) { this.logger.debug(`sdk emit | ${eventName} | ${JSON.stringify(ext)}`); // 如果出现异常,则将通话状态置为结束 if (eventName === CTIEvent.OnCtiError) { this._callStatus = CallStatus.Stopped; } try { this.emit(eventName, ext); console.log(error); } catch (error) { this.logger.error(`业务监听 ${eventName} 事件,处理回调时报错: ${error}`); } } /** @private checkIn 服务端签入,CTIStatus Ready 时自动调用,坐席状态变更 */ checkIn() { return __awaiter(this, void 0, void 0, function* () { const res = yield agentCheckIn(this._baseParams); if (res.code === 0) { this.eventEmitAndTrack(CTIEvent.OnInitalSuccess, { saas_id: this.saas_id, agent_id: this.agent_id, scene: this.scene, phoneNum: this._initOptions.phone_num, sipServer: this._initOptions.sip_server }); } return res; }); } /** @public checkOut 服务端签出, unInit 时自动调用,坐席状态变更 */ checkOut() { return __awaiter(this, void 0, void 0, function* () { return yield agentCheckOut(this._baseParams); }); } /** @private _getCtiFlowId 获取手动外呼场景需要的 ctiFlowId */ getCtiFlowId() { return __awaiter(this, void 0, void 0, function* () { const res = yield getCtiFlowId(this._baseParams); const { code, data } = res; if (code === 0) { this._baseParams.ctiFlowId = data; this._ctiFlowIdList.push(data); setBaseOption(BaseOption.TrackParams, { cti_flow_id: data, cti_flow_id_list: JSON.stringify(this._ctiFlowIdList) }); } return res; }); } /** @public setIdle 服务端置闲,坐席状态变更 */ setIdle() { return __awaiter(this, void 0, void 0, function* () { return yield agentSetIdle(this._baseParams); }); } /** @public setBusy 服务端置忙,坐席状态变更 */ setBusy() { return __awaiter(this, void 0, void 0, function* () { return yield agentSetBusy(this._baseParams); }); } makeCall(params) { return __awaiter(this, void 0, void 0, function* () { // 如果当前通话状态处于外呼开始,则不允许再次调用此方法并上报埋点 if (this._callStatus === CallStatus.Started) { return; } this._callStatus = CallStatus.Started; yield this.getCtiFlowId(); this.playAudio(AudioName.WaitAudio); return yield this.serverCall(params); }); } serverCall({ called, caller, ext }) { return __awaiter(this, void 0, void 0, function* () { const params = Object.assign(Object.assign(Object.assign({}, this._baseParams), { called, caller }), ext); const res = yield manualCall(params); const { code, data } = res; if (code === 0) { if (this._callId === '' && data) { this._callId = data; setBaseOption(BaseOption.TrackParams, { call_id: data }); } } else { // 外呼接口调用失败将通话状态置为结束不阻碍下次外呼 this._callStatus = CallStatus.Stopped; this.stopLocalAudio(); } return res; }); } /** @public answer SDK SIP 接起 */ answer() { return new Promise((resolve, reject) => { var _a; (_a = this._sipUserAgent) === null || _a === void 0 ? void 0 : _a.answer({ sessionDescriptionHandlerOptions: { constraints: { audio: true, video: false } } }).then(() => { resolve({ code: 0, data: 'answer', msg: 'SIP 接起电话成功' }); this.logger.debug('sip_accept_success'); }).catch(err => { const errorData = { type: CTIErrorType.SdkError, code: SdkErrorCode.Answer, msg: this.scene === Scene.Manual ? ExceptMessage.ManualCallAnswerErrorMsg : ExceptMessage.RobotOrWeChatAnswerErrorMsg, method: 'answer' }; reject(errorData); this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData, `${err}`); this.logger.error(`${CTIEvent.OnCtiError} | ${err}`); }); }); } /** @public bye SDK SIP 挂断 */ bye() { return new Promise((resolve, reject) => { var _a; (_a = this._sipUserAgent) === null || _a === void 0 ? void 0 : _a.hangup().then(() => { resolve({ code: 0, data: 'bye', msg: 'SIP 挂断电话成功' }); this.logger.debug('sip_bye_success'); }).catch(err => { const errorData = { type: CTIErrorType.SdkError, code: SdkErrorCode.Bye, msg: ExceptMessage.SipByeErrorMsg, method: 'bye' }; reject(errorData); if (err.message !== 'Session does not exist.') { this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData, `${err}`); } this.logger.error(`${CTIEvent.OnCtiError} | ${err}`); }); }); } /** @public serverBye 挂断且流转坐席状态 */ serverBye() { return __awaiter(this, void 0, void 0, function* () { try { this.bye(); // 如果人工外呼场景下,没有flowId则不调用并上报埋点 if (!this._baseParams.ctiFlowId && this.scene === Scene.Manual) { return; } const res = yield this.turnHang(); return Promise.resolve(res); } catch (error) { return Promise.reject(error); } }); } /** @public turnHang 流转坐席状态-通话结束 */ turnHang() { return __awaiter(this, void 0, void 0, function* () { const res = yield manualHang(Object.assign(Object.assign({}, this._baseParams), { callId: this._callId })); this._callId = ''; return res; }); } /** @public getAgentStatus 获取坐席状态 */ getAgentStatus() { return __awaiter(this, void 0, void 0, function* () { return yield getAgentStatus(this._baseParams); }); } /** * @public loadAgentGroupData 监听-根据监听组 ID 获取监听组成员 * @param {string[]} monitorIds */ loadAgentGroupData(monitorIds) { return __awaiter(this, void 0, void 0, function* () { return yield loadAgentGroupData(Object.assign(Object.assign({}, this._baseParams), { monitorIds })); }); } /** * @public listen 监听-服务端发起监听 * @param {string} monitoredAgNo */ listen(monitoredAgNo) { return __awaiter(this, void 0, void 0, function* () { const flowIdRes = yield this.getCtiFlowId(); if (flowIdRes && flowIdRes.code !== 0) { return flowIdRes; } return yield listen(Object.assign(Object.assign({}, this._baseParams), { agent_id: monitoredAgNo, leaderAgentId: this.agent_id })); }); } /** * @public setActiveService 机器人外呼-签入人工组 * @param {string} serviceId */ setActiveService(serviceId) { return __awaiter(this, void 0, void 0, function* () { return yield setActiveServiceTask(Object.assign(Object.assign({}, this._baseParams), { serviceId })); }); } /** @public unInit 卸载 SDK,checkOut 成功后断开 socket 和 sip 连接,并销毁 SdCTI 实例 */ unInit() { return __awaiter(this, void 0, void 0, function* () { yield this.checkOut(); this.setCTIStatus(CTIStatus.Terminated); this.initInstanceOptions(); this.clearSocketAndSip(); this._callId = ''; }); } } __decorate([getUserMedia()], HsCTI.prototype, "init", null); __decorate([checkCTIStatus('签入'), handleApiRes()], HsCTI.prototype, "checkIn", null); __decorate([checkCTIStatus('签出'), handleApiRes()], HsCTI.prototype, "checkOut", null); __decorate([handleApiRes()], HsCTI.prototype, "getCtiFlowId", null); __decorate([checkCTIStatus('置闲'), handleApiRes()], HsCTI.prototype, "setIdle", null); __decorate([checkCTIStatus('置忙'), handleApiRes()], HsCTI.prototype, "setBusy", null); __decorate([checkCTIStatus('人工外呼')], HsCTI.prototype, "makeCall", null); __decorate([handleApiRes()], HsCTI.prototype, "serverCall", null); __decorate([checkCTIStatus('接起')], HsCTI.prototype, "answer", null); __decorate([checkCTIStatus('挂断')], HsCTI.prototype, "bye", null); __decorate([handleApiRes()], HsCTI.prototype, "turnHang", null); __decorate([handleApiRes()], HsCTI.prototype, "getAgentStatus", null); __decorate([checkCTIStatus('获取监听组成员'), handleApiRes()], HsCTI.prototype, "loadAgentGroupData", null); __decorate([checkCTIStatus('坐席监听'), handleApiRes()], HsCTI.prototype, "listen", null); __decorate([checkCTIStatus('签入人工组'), handleApiRes()], HsCTI.prototype, "setActiveService", null); __decorate([validateParams()], HsCTI, "getInstance", null); /** * @function getInstance 获取 HsCTI 的实例 * @param HsCTIInitOptions 初始化 HsCTI 的配置 */ const getInstance = HsCTIInitOptions => HsCTI.getInstance(HsCTIInitOptions); export { CTIErrorType, CTIEvent, CTIStatus, CallStatus, HskTerminatedCode, Logger, LoggerLevels, MonitorScene, SIPStatus, Scene, SdkErrorCode, SessionStatus, SocketStatus, getInstance };