hs-cti.es6.esm.js 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227
  1. /*!
  2. - Name HS_CTI
  3. - FileName hs-cti
  4. - Version 1.0.9
  5. - JS Standard es6
  6. - Author platformfe
  7. - Built on 2024/11/27 10:37:38
  8. - GitHub
  9. - Branch main
  10. - CommitID 987d86cd91cde7d1c235ad106d79fc92c305b8ff
  11. - CommitMessage fix: 修改socket地址
  12. */
  13. import { Web, UserAgent, UserAgentState, Registerer, RegistererState, Inviter, Invitation, Session, Messager, RequestPendingError, SessionState } from 'sip.js';
  14. import io from 'socket.io-client';
  15. /******************************************************************************
  16. Copyright (c) Microsoft Corporation.
  17. Permission to use, copy, modify, and/or distribute this software for any
  18. purpose with or without fee is hereby granted.
  19. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  20. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  21. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  22. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  23. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  24. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  25. PERFORMANCE OF THIS SOFTWARE.
  26. ***************************************************************************** */
  27. /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
  28. function __decorate(decorators, target, key, desc) {
  29. var c = arguments.length,
  30. r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
  31. d;
  32. 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;
  33. return c > 3 && r && Object.defineProperty(target, key, r), r;
  34. }
  35. function __awaiter(thisArg, _arguments, P, generator) {
  36. function adopt(value) {
  37. return value instanceof P ? value : new P(function (resolve) {
  38. resolve(value);
  39. });
  40. }
  41. return new (P || (P = Promise))(function (resolve, reject) {
  42. function fulfilled(value) {
  43. try {
  44. step(generator.next(value));
  45. } catch (e) {
  46. reject(e);
  47. }
  48. }
  49. function rejected(value) {
  50. try {
  51. step(generator["throw"](value));
  52. } catch (e) {
  53. reject(e);
  54. }
  55. }
  56. function step(result) {
  57. result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
  58. }
  59. step((generator = generator.apply(thisArg, _arguments || [])).next());
  60. });
  61. }
  62. typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
  63. var e = new Error(message);
  64. return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
  65. };
  66. /**
  67. * A session manager for SIP.js sessions.
  68. * @public
  69. */
  70. class SessionManagerPlus {
  71. /**
  72. * Constructs a new instance of the `SessionManager` class.
  73. * @param server - SIP WebSocket Server URL.
  74. * @param options - Options bucket. See {@link Web.SessionManagerOptions} for details.
  75. */
  76. constructor(server, options = {}) {
  77. /** Delegate. */
  78. Object.defineProperty(this, "delegate", {
  79. enumerable: true,
  80. configurable: true,
  81. writable: true,
  82. value: void 0
  83. });
  84. /** Sessions being managed. */
  85. Object.defineProperty(this, "managedSessions", {
  86. enumerable: true,
  87. configurable: true,
  88. writable: true,
  89. value: []
  90. });
  91. /** User agent which created sessions being managed. */
  92. Object.defineProperty(this, "userAgent", {
  93. enumerable: true,
  94. configurable: true,
  95. writable: true,
  96. value: void 0
  97. });
  98. Object.defineProperty(this, "logger", {
  99. enumerable: true,
  100. configurable: true,
  101. writable: true,
  102. value: void 0
  103. });
  104. Object.defineProperty(this, "options", {
  105. enumerable: true,
  106. configurable: true,
  107. writable: true,
  108. value: void 0
  109. });
  110. Object.defineProperty(this, "optionsPingFailure", {
  111. enumerable: true,
  112. configurable: true,
  113. writable: true,
  114. value: false
  115. });
  116. Object.defineProperty(this, "optionsPingRequest", {
  117. enumerable: true,
  118. configurable: true,
  119. writable: true,
  120. value: void 0
  121. });
  122. Object.defineProperty(this, "optionsPingRunning", {
  123. enumerable: true,
  124. configurable: true,
  125. writable: true,
  126. value: false
  127. });
  128. Object.defineProperty(this, "optionsPingTimeout", {
  129. enumerable: true,
  130. configurable: true,
  131. writable: true,
  132. value: void 0
  133. });
  134. Object.defineProperty(this, "registrationAttemptTimeout", {
  135. enumerable: true,
  136. configurable: true,
  137. writable: true,
  138. value: void 0
  139. });
  140. Object.defineProperty(this, "registerer", {
  141. enumerable: true,
  142. configurable: true,
  143. writable: true,
  144. value: void 0
  145. });
  146. Object.defineProperty(this, "registererOptions", {
  147. enumerable: true,
  148. configurable: true,
  149. writable: true,
  150. value: void 0
  151. });
  152. Object.defineProperty(this, "registererRegisterOptions", {
  153. enumerable: true,
  154. configurable: true,
  155. writable: true,
  156. value: void 0
  157. });
  158. Object.defineProperty(this, "shouldBeConnected", {
  159. enumerable: true,
  160. configurable: true,
  161. writable: true,
  162. value: false
  163. });
  164. Object.defineProperty(this, "shouldBeRegistered", {
  165. enumerable: true,
  166. configurable: true,
  167. writable: true,
  168. value: false
  169. });
  170. Object.defineProperty(this, "attemptingReconnection", {
  171. enumerable: true,
  172. configurable: true,
  173. writable: true,
  174. value: false
  175. });
  176. // Delegate
  177. this.delegate = options.delegate;
  178. // Copy options
  179. this.options = Object.assign({
  180. aor: '',
  181. autoStop: true,
  182. delegate: {},
  183. iceStopWaitingOnServerReflexive: false,
  184. managedSessionFactory: this.managedSessionFactory,
  185. maxSimultaneousSessions: 2,
  186. media: {},
  187. optionsPingInterval: -1,
  188. optionsPingRequestURI: '',
  189. reconnectionAttempts: 3,
  190. reconnectionDelay: 3,
  191. registrationRetry: false,
  192. registrationRetryInterval: 3,
  193. registerGuard: null,
  194. registererOptions: {},
  195. registererRegisterOptions: {},
  196. sendDTMFUsingSessionDescriptionHandler: false,
  197. userAgentOptions: {}
  198. }, SessionManagerPlus.stripUndefinedProperties(options));
  199. // UserAgentOptions
  200. const userAgentOptions = Object.assign({}, options.userAgentOptions);
  201. // Transport
  202. if (!userAgentOptions.transportConstructor) {
  203. userAgentOptions.transportConstructor = Web.Transport;
  204. }
  205. // TransportOptions
  206. if (!userAgentOptions.transportOptions) {
  207. userAgentOptions.transportOptions = {
  208. server
  209. };
  210. }
  211. // URI
  212. if (!userAgentOptions.uri) {
  213. // If an AOR was provided, convert it to a URI
  214. if (options.aor) {
  215. const uri = UserAgent.makeURI(options.aor);
  216. if (!uri) {
  217. throw new Error(`Failed to create valid URI from ${options.aor}`);
  218. }
  219. userAgentOptions.uri = uri;
  220. }
  221. }
  222. // UserAgent
  223. this.userAgent = new UserAgent(userAgentOptions);
  224. // UserAgent's delegate
  225. this.userAgent.delegate = {
  226. // Handle connection with server established
  227. onConnect: () => {
  228. this.logger.log(`Connected`);
  229. if (this.delegate && this.delegate.onServerConnect) {
  230. this.delegate.onServerConnect();
  231. }
  232. // Attempt to register if we are supposed to be registered
  233. if (this.shouldBeRegistered) {
  234. this.register();
  235. }
  236. // Start OPTIONS pings if we are to be pinging
  237. if (this.options.optionsPingInterval > 0) {
  238. this.optionsPingStart();
  239. }
  240. },
  241. // Handle connection with server lost
  242. onDisconnect: error => __awaiter(this, void 0, void 0, function* () {
  243. var _a, _b, _c, _d;
  244. this.logger.log(`Disconnected`);
  245. // Stop OPTIONS ping if need be.
  246. let optionsPingFailure = false;
  247. if (this.options.optionsPingInterval > 0) {
  248. optionsPingFailure = this.optionsPingFailure;
  249. this.optionsPingFailure = false;
  250. this.optionsPingStop();
  251. }
  252. // If the user called `disconnect` a graceful cleanup will be done therein.
  253. // Only cleanup if network/server dropped the connection.
  254. // Only reconnect if network/server dropped the connection
  255. if (error || optionsPingFailure) {
  256. // There is no transport at this point, so we are not expecting to be able to
  257. // send messages much less get responses. So just dispose of everything without
  258. // waiting for anything to succeed.
  259. if (this.registerer) {
  260. this.logger.log(`Disposing of registerer...`);
  261. this.registerer.dispose().catch(e => {
  262. this.logger.debug(`Error occurred disposing of registerer after connection with server was lost.`);
  263. this.logger.debug(e.toString());
  264. });
  265. this.registerer = undefined;
  266. }
  267. this.managedSessions.slice().map(el => el.session).forEach(session => __awaiter(this, void 0, void 0, function* () {
  268. this.logger.log(`Disposing of session...`);
  269. session.dispose().catch(e => {
  270. this.logger.debug(`Error occurred disposing of a session after connection with server was lost.`);
  271. this.logger.debug(e.toString());
  272. });
  273. }));
  274. // Attempt to reconnect if we are supposed to be connected.
  275. if (this.shouldBeConnected) {
  276. (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectStart) === null || _b === void 0 ? void 0 : _b.call(_a);
  277. this.attemptReconnection();
  278. }
  279. } else {
  280. // Let delgate know we have disconnected
  281. (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onServerDisconnect) === null || _d === void 0 ? void 0 : _d.call(_c, error);
  282. }
  283. }),
  284. // Handle incoming invitations
  285. onInvite: invitation => {
  286. this.logger.log(`[${invitation.id}] Received INVITE`);
  287. // Guard against a maximum number of pre-existing sessions.
  288. // An incoming INVITE request may be received at any time and/or while in the process
  289. // of sending an outgoing INVITE request. So we reject any incoming INVITE in those cases.
  290. const maxSessions = this.options.maxSimultaneousSessions;
  291. if (maxSessions !== 0 && this.managedSessions.length > maxSessions) {
  292. this.logger.warn(`[${invitation.id}] Session already in progress, rejecting INVITE...`);
  293. invitation.reject().then(() => {
  294. this.logger.log(`[${invitation.id}] Rejected INVITE`);
  295. }).catch(error => {
  296. this.logger.error(`[${invitation.id}] Failed to reject INVITE`);
  297. this.logger.error(error.toString());
  298. });
  299. return;
  300. }
  301. // Use our configured constraints as options for any Inviter created as result of a REFER
  302. const referralInviterOptions = {
  303. sessionDescriptionHandlerOptions: {
  304. constraints: this.constraints
  305. }
  306. };
  307. // Initialize our session
  308. this.initSession(invitation, referralInviterOptions);
  309. // Delegate
  310. if (this.delegate && this.delegate.onCallReceived) {
  311. this.delegate.onCallReceived(invitation);
  312. } else {
  313. this.logger.warn(`[${invitation.id}] No handler available, rejecting INVITE...`);
  314. invitation.reject().then(() => {
  315. this.logger.log(`[${invitation.id}] Rejected INVITE`);
  316. }).catch(error => {
  317. this.logger.error(`[${invitation.id}] Failed to reject INVITE`);
  318. this.logger.error(error.toString());
  319. });
  320. }
  321. },
  322. // Handle incoming messages
  323. onMessage: message => {
  324. message.accept().then(() => {
  325. if (this.delegate && this.delegate.onMessageReceived) {
  326. this.delegate.onMessageReceived(message);
  327. }
  328. });
  329. },
  330. // Handle incoming notifications
  331. onNotify: notification => {
  332. notification.accept().then(() => {
  333. if (this.delegate && this.delegate.onNotificationReceived) {
  334. this.delegate.onNotificationReceived(notification);
  335. }
  336. });
  337. }
  338. };
  339. // RegistererOptions
  340. this.registererOptions = Object.assign({}, options.registererOptions);
  341. // RegistererRegisterOptions
  342. this.registererRegisterOptions = Object.assign({}, options.registererRegisterOptions);
  343. // Retry registration on failure or rejection.
  344. if (this.options.registrationRetry) {
  345. // If the register request is rejected, try again...
  346. this.registererRegisterOptions.requestDelegate = this.registererRegisterOptions.requestDelegate || {};
  347. const existingOnReject = this.registererRegisterOptions.requestDelegate.onReject;
  348. this.registererRegisterOptions.requestDelegate.onReject = response => {
  349. existingOnReject && existingOnReject(response);
  350. // If at first we don't succeed, try try again...
  351. this.attemptRegistration();
  352. };
  353. }
  354. // Use the SIP.js logger
  355. this.logger = this.userAgent.getLogger('sip.SessionManager');
  356. // Monitor network connectivity and attempt reconnection and reregistration when we come online
  357. window.addEventListener('online', () => {
  358. this.logger.log(`Online`);
  359. if (this.shouldBeConnected) {
  360. this.connect();
  361. }
  362. });
  363. // NOTE: The autoStop option does not currently work as one likley expects.
  364. // This code is here because the "autoStop behavior" and this assoicated
  365. // implemenation has been a recurring request. So instead of removing
  366. // the implementation again (because it doesn't work) and then having
  367. // to explain agian the issue over and over again to those who want it,
  368. // we have included it here to break that cycle. The implementation is
  369. // harmless and serves to provide an explaination for those interested.
  370. if (this.options.autoStop) {
  371. // Standard operation workflow will resume after this callback exits, meaning
  372. // that any asynchronous operations are likely not going to be finished, especially
  373. // if they are guaranteed to not be executed in the current tick (promises fall
  374. // under this category, they will never be resolved synchronously by design).
  375. window.addEventListener('beforeunload', () => __awaiter(this, void 0, void 0, function* () {
  376. this.shouldBeConnected = false;
  377. this.shouldBeRegistered = false;
  378. if (this.userAgent.state !== UserAgentState.Stopped) {
  379. // The stop() method returns a promise which will not resolve before the page unloads.
  380. yield this.userAgent.stop();
  381. }
  382. }));
  383. }
  384. }
  385. /**
  386. * Strip properties with undefined values from options.
  387. * This is a work around while waiting for missing vs undefined to be addressed (or not)...
  388. * https://github.com/Microsoft/TypeScript/issues/13195
  389. * @param options - Options to reduce
  390. */
  391. static stripUndefinedProperties(options) {
  392. return Object.keys(options).reduce((object, key) => {
  393. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  394. if (options[key] !== undefined) {
  395. object[key] = options[key];
  396. }
  397. return object;
  398. }, {});
  399. }
  400. /**
  401. * The local media stream. Undefined if call not answered.
  402. * @param session - Session to get the media stream from.
  403. */
  404. getLocalMediaStream(session) {
  405. const hsh = session.sessionDescriptionHandler;
  406. if (!hsh) {
  407. return undefined;
  408. }
  409. if (!(hsh instanceof Web.SessionDescriptionHandler)) {
  410. throw new Error('Session description handler not instance of web SessionDescriptionHandler');
  411. }
  412. return hsh.localMediaStream;
  413. }
  414. /**
  415. * The remote media stream. Undefined if call not answered.
  416. * @param session - Session to get the media stream from.
  417. */
  418. getRemoteMediaStream(session) {
  419. const hsh = session.sessionDescriptionHandler;
  420. if (!hsh) {
  421. return undefined;
  422. }
  423. if (!(hsh instanceof Web.SessionDescriptionHandler)) {
  424. throw new Error('Session description handler not instance of web SessionDescriptionHandler');
  425. }
  426. return hsh.remoteMediaStream;
  427. }
  428. /**
  429. * The local audio track, if available.
  430. * @param session - Session to get track from.
  431. * @deprecated Use localMediaStream and get track from the stream.
  432. */
  433. getLocalAudioTrack(session) {
  434. var _a;
  435. return (_a = this.getLocalMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'audio');
  436. }
  437. /**
  438. * The local video track, if available.
  439. * @param session - Session to get track from.
  440. * @deprecated Use localMediaStream and get track from the stream.
  441. */
  442. getLocalVideoTrack(session) {
  443. var _a;
  444. return (_a = this.getLocalMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'video');
  445. }
  446. /**
  447. * The remote audio track, if available.
  448. * @param session - Session to get track from.
  449. * @deprecated Use remoteMediaStream and get track from the stream.
  450. */
  451. getRemoteAudioTrack(session) {
  452. var _a;
  453. return (_a = this.getRemoteMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'audio');
  454. }
  455. /**
  456. * The remote video track, if available.
  457. * @param session - Session to get track from.
  458. * @deprecated Use remoteMediaStream and get track from the stream.
  459. */
  460. getRemoteVideoTrack(session) {
  461. var _a;
  462. return (_a = this.getRemoteMediaStream(session)) === null || _a === void 0 ? void 0 : _a.getTracks().find(track => track.kind === 'video');
  463. }
  464. /**
  465. * Connect.
  466. * @remarks
  467. * If not started, starts the UserAgent connecting the WebSocket Transport.
  468. * Otherwise reconnects the UserAgent's WebSocket Transport.
  469. * Attempts will be made to reconnect as needed.
  470. */
  471. connect() {
  472. return __awaiter(this, void 0, void 0, function* () {
  473. this.logger.log(`Connecting UserAgent...`);
  474. this.shouldBeConnected = true;
  475. if (this.userAgent.state !== UserAgentState.Started) {
  476. return this.userAgent.start();
  477. }
  478. return this.userAgent.reconnect();
  479. });
  480. }
  481. /**
  482. * Disconnect.
  483. * @remarks
  484. * If not stopped, stops the UserAgent disconnecting the WebSocket Transport.
  485. */
  486. disconnect() {
  487. return __awaiter(this, void 0, void 0, function* () {
  488. this.logger.log(`Disconnecting UserAgent...`);
  489. if (this.userAgent.state === UserAgentState.Stopped) {
  490. return Promise.resolve();
  491. }
  492. this.shouldBeConnected = false;
  493. this.shouldBeRegistered = false;
  494. this.registerer = undefined;
  495. return this.userAgent.stop();
  496. });
  497. }
  498. /**
  499. * Return true if transport is connected.
  500. */
  501. isConnected() {
  502. return this.userAgent.isConnected();
  503. }
  504. /**
  505. * Start receiving incoming calls.
  506. * @remarks
  507. * Send a REGISTER request for the UserAgent's AOR.
  508. * Resolves when the REGISTER request is sent, otherwise rejects.
  509. * Attempts will be made to re-register as needed.
  510. */
  511. register(registererRegisterOptions) {
  512. return __awaiter(this, void 0, void 0, function* () {
  513. this.logger.log(`Registering UserAgent...`);
  514. this.shouldBeRegistered = true;
  515. if (registererRegisterOptions !== undefined) {
  516. this.registererRegisterOptions = Object.assign({}, registererRegisterOptions);
  517. }
  518. if (!this.registerer) {
  519. this.registerer = new Registerer(this.userAgent, this.registererOptions);
  520. this.registerer.stateChange.addListener(state => {
  521. switch (state) {
  522. case RegistererState.Initial:
  523. break;
  524. case RegistererState.Registered:
  525. if (this.delegate && this.delegate.onRegistered) {
  526. this.delegate.onRegistered();
  527. }
  528. break;
  529. case RegistererState.Unregistered:
  530. if (this.delegate && this.delegate.onUnregistered) {
  531. this.delegate.onUnregistered();
  532. }
  533. // If we transition to an unregister state, attempt to get back to a registered state.
  534. if (this.shouldBeRegistered) {
  535. this.attemptRegistration();
  536. }
  537. break;
  538. case RegistererState.Terminated:
  539. break;
  540. default:
  541. throw new Error('Unknown registerer state.');
  542. }
  543. });
  544. }
  545. return this.attemptRegistration(true);
  546. });
  547. }
  548. /**
  549. * Stop receiving incoming calls.
  550. * @remarks
  551. * Send an un-REGISTER request for the UserAgent's AOR.
  552. * Resolves when the un-REGISTER request is sent, otherwise rejects.
  553. */
  554. unregister(registererUnregisterOptions) {
  555. return __awaiter(this, void 0, void 0, function* () {
  556. this.logger.log(`Unregistering UserAgent...`);
  557. this.shouldBeRegistered = false;
  558. if (!this.registerer) {
  559. this.logger.warn(`No registerer to unregister.`);
  560. return Promise.resolve();
  561. }
  562. return this.registerer.unregister(registererUnregisterOptions).then(() => {
  563. return;
  564. });
  565. });
  566. }
  567. /**
  568. * Make an outgoing call.
  569. * @remarks
  570. * Send an INVITE request to create a new Session.
  571. * Resolves when the INVITE request is sent, otherwise rejects.
  572. * Use `onCallAnswered` delegate method to determine if Session is established.
  573. * @param destination - The target destination to call. A SIP address to send the INVITE to.
  574. * @param inviterOptions - Optional options for Inviter constructor.
  575. * @param inviterInviteOptions - Optional options for Inviter.invite().
  576. */
  577. call(destination, inviterOptions, inviterInviteOptions) {
  578. return __awaiter(this, void 0, void 0, function* () {
  579. this.logger.log(`Beginning Session...`);
  580. // Guard against a maximum number of pre-existing sessions.
  581. // An incoming INVITE request may be received at any time and/or while in the process
  582. // of sending an outgoing INVITE request. So we reject any incoming INVITE in those cases.
  583. const maxSessions = this.options.maxSimultaneousSessions;
  584. if (maxSessions !== 0 && this.managedSessions.length > maxSessions) {
  585. return Promise.reject(new Error('Maximum number of sessions already exists.'));
  586. }
  587. const target = UserAgent.makeURI(destination);
  588. if (!target) {
  589. return Promise.reject(new Error(`Failed to create a valid URI from "${destination}"`));
  590. }
  591. // Use our configured constraints as InviterOptions if none provided
  592. if (!inviterOptions) {
  593. inviterOptions = {};
  594. }
  595. if (!inviterOptions.sessionDescriptionHandlerOptions) {
  596. inviterOptions.sessionDescriptionHandlerOptions = {};
  597. }
  598. if (!inviterOptions.sessionDescriptionHandlerOptions.constraints) {
  599. inviterOptions.sessionDescriptionHandlerOptions.constraints = this.constraints;
  600. }
  601. // If utilizing early media, add a handler to catch 183 Session Progress
  602. // messages and then to play the associated remote media (the early media).
  603. if (inviterOptions.earlyMedia) {
  604. inviterInviteOptions = inviterInviteOptions || {};
  605. inviterInviteOptions.requestDelegate = inviterInviteOptions.requestDelegate || {};
  606. const existingOnProgress = inviterInviteOptions.requestDelegate.onProgress;
  607. inviterInviteOptions.requestDelegate.onProgress = response => {
  608. if (response.message.statusCode === 183) {
  609. this.setupRemoteMedia(inviter);
  610. }
  611. existingOnProgress && existingOnProgress(response);
  612. };
  613. }
  614. // TODO: Any existing onSessionDescriptionHandler is getting clobbered here.
  615. // If we get a server reflexive candidate, stop waiting on ICE gathering to complete.
  616. // The candidate is a server reflexive candidate; the ip indicates an intermediary
  617. // address assigned by the STUN server to represent the candidate's peer anonymously.
  618. if (this.options.iceStopWaitingOnServerReflexive) {
  619. inviterOptions.delegate = inviterOptions.delegate || {};
  620. inviterOptions.delegate.onSessionDescriptionHandler = sessionDescriptionHandler => {
  621. if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) {
  622. throw new Error('Session description handler not instance of SessionDescriptionHandler');
  623. }
  624. sessionDescriptionHandler.peerConnectionDelegate = {
  625. onicecandidate: event => {
  626. var _a;
  627. if (((_a = event.candidate) === null || _a === void 0 ? void 0 : _a.type) === 'srflx') {
  628. this.logger.log(`[${inviter.id}] Found srflx ICE candidate, stop waiting...`);
  629. // In sip.js > 0.20.1 this cast should be removed as iceGatheringComplete will be public
  630. const hsh = sessionDescriptionHandler;
  631. hsh.iceGatheringComplete();
  632. }
  633. }
  634. };
  635. };
  636. }
  637. // Create a new Inviter for the outgoing Session
  638. const inviter = new Inviter(this.userAgent, target, inviterOptions);
  639. // Send INVITE
  640. return this.sendInvite(inviter, inviterOptions, inviterInviteOptions).then(() => {
  641. return inviter;
  642. });
  643. });
  644. }
  645. /**
  646. * Hangup a call.
  647. * @param session - Session to hangup.
  648. * @remarks
  649. * Send a BYE request, CANCEL request or reject response to end the current Session.
  650. * Resolves when the request/response is sent, otherwise rejects.
  651. * Use `onCallHangup` delegate method to determine if and when call is ended.
  652. */
  653. hangup(session) {
  654. return __awaiter(this, void 0, void 0, function* () {
  655. this.logger.log(`[${session.id}] Hangup...`);
  656. if (!this.sessionExists(session)) {
  657. return Promise.reject(new Error('Session does not exist.'));
  658. }
  659. return this.terminate(session);
  660. });
  661. }
  662. /**
  663. * Answer an incoming call.
  664. * @param session - Session to answer.
  665. * @remarks
  666. * Accept an incoming INVITE request creating a new Session.
  667. * Resolves with the response is sent, otherwise rejects.
  668. * Use `onCallAnswered` delegate method to determine if and when call is established.
  669. * @param invitationAcceptOptions - Optional options for Inviter.accept().
  670. */
  671. answer(session, invitationAcceptOptions) {
  672. return __awaiter(this, void 0, void 0, function* () {
  673. this.logger.log(`[${session.id}] Accepting Invitation...`);
  674. if (!this.sessionExists(session)) {
  675. return Promise.reject(new Error('Session does not exist.'));
  676. }
  677. if (!(session instanceof Invitation)) {
  678. return Promise.reject(new Error('Session not instance of Invitation.'));
  679. }
  680. // Use our configured constraints as InvitationAcceptOptions if none provided
  681. if (!invitationAcceptOptions) {
  682. invitationAcceptOptions = {};
  683. }
  684. if (!invitationAcceptOptions.sessionDescriptionHandlerOptions) {
  685. invitationAcceptOptions.sessionDescriptionHandlerOptions = {};
  686. }
  687. if (!invitationAcceptOptions.sessionDescriptionHandlerOptions.constraints) {
  688. invitationAcceptOptions.sessionDescriptionHandlerOptions.constraints = this.constraints;
  689. }
  690. return session.accept(invitationAcceptOptions);
  691. });
  692. }
  693. /**
  694. * Decline an incoming call.
  695. * @param session - Session to decline.
  696. * @remarks
  697. * Reject an incoming INVITE request.
  698. * Resolves with the response is sent, otherwise rejects.
  699. * Use `onCallHangup` delegate method to determine if and when call is ended.
  700. */
  701. decline(session) {
  702. return __awaiter(this, void 0, void 0, function* () {
  703. this.logger.log(`[${session.id}] Rejecting Invitation...`);
  704. if (!this.sessionExists(session)) {
  705. return Promise.reject(new Error('Session does not exist.'));
  706. }
  707. if (!(session instanceof Invitation)) {
  708. return Promise.reject(new Error('Session not instance of Invitation.'));
  709. }
  710. return session.reject();
  711. });
  712. }
  713. /**
  714. * Hold call
  715. * @param session - Session to hold.
  716. * @remarks
  717. * Send a re-INVITE with new offer indicating "hold".
  718. * Resolves when the re-INVITE request is sent, otherwise rejects.
  719. * Use `onCallHold` delegate method to determine if request is accepted or rejected.
  720. * See: https://tools.ietf.org/html/rfc6337
  721. */
  722. hold(session) {
  723. return __awaiter(this, void 0, void 0, function* () {
  724. this.logger.log(`[${session.id}] Holding session...`);
  725. return this.setHold(session, true);
  726. });
  727. }
  728. /**
  729. * Unhold call.
  730. * @param session - Session to unhold.
  731. * @remarks
  732. * Send a re-INVITE with new offer indicating "unhold".
  733. * Resolves when the re-INVITE request is sent, otherwise rejects.
  734. * Use `onCallHold` delegate method to determine if request is accepted or rejected.
  735. * See: https://tools.ietf.org/html/rfc6337
  736. */
  737. unhold(session) {
  738. return __awaiter(this, void 0, void 0, function* () {
  739. this.logger.log(`[${session.id}] Unholding session...`);
  740. return this.setHold(session, false);
  741. });
  742. }
  743. /**
  744. * Hold state.
  745. * @param session - Session to check.
  746. * @remarks
  747. * True if session is on hold.
  748. */
  749. isHeld(session) {
  750. const managedSession = this.sessionManaged(session);
  751. return managedSession ? managedSession.held : false;
  752. }
  753. /**
  754. * Mute call.
  755. * @param session - Session to mute.
  756. * @remarks
  757. * Disable sender's media tracks.
  758. */
  759. mute(session) {
  760. this.logger.log(`[${session.id}] Disabling media tracks...`);
  761. this.setMute(session, true);
  762. }
  763. /**
  764. * Unmute call.
  765. * @param session - Session to unmute.
  766. * @remarks
  767. * Enable sender's media tracks.
  768. */
  769. unmute(session) {
  770. this.logger.log(`[${session.id}] Enabling media tracks...`);
  771. this.setMute(session, false);
  772. }
  773. /**
  774. * Mute state.
  775. * @param session - Session to check.
  776. * @remarks
  777. * True if sender's media track is disabled.
  778. */
  779. isMuted(session) {
  780. const managedSession = this.sessionManaged(session);
  781. return managedSession ? managedSession.muted : false;
  782. }
  783. /**
  784. * Send DTMF.
  785. * @param session - Session to send on.
  786. * @remarks
  787. * Send an INFO request with content type application/dtmf-relay.
  788. * @param tone - Tone to send.
  789. */
  790. sendDTMF(session, tone) {
  791. return __awaiter(this, void 0, void 0, function* () {
  792. this.logger.log(`[${session.id}] Sending DTMF...`);
  793. // Validate tone
  794. if (!/^[0-9A-D#*,]$/.exec(tone)) {
  795. return Promise.reject(new Error('Invalid DTMF tone.'));
  796. }
  797. if (!this.sessionExists(session)) {
  798. return Promise.reject(new Error('Session does not exist.'));
  799. }
  800. this.logger.log(`[${session.id}] Sending DTMF tone: ${tone}`);
  801. if (this.options.sendDTMFUsingSessionDescriptionHandler) {
  802. if (!session.sessionDescriptionHandler) {
  803. return Promise.reject(new Error('Session desciption handler undefined.'));
  804. }
  805. if (!session.sessionDescriptionHandler.sendDtmf(tone)) {
  806. return Promise.reject(new Error('Failed to send DTMF'));
  807. }
  808. return Promise.resolve();
  809. } else {
  810. // As RFC 6086 states, sending DTMF via INFO is not standardized...
  811. //
  812. // Companies have been using INFO messages in order to transport
  813. // Dual-Tone Multi-Frequency (DTMF) tones. All mechanisms are
  814. // proprietary and have not been standardized.
  815. // https://tools.ietf.org/html/rfc6086#section-2
  816. //
  817. // It is however widely supported based on this draft:
  818. // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00
  819. // The UA MUST populate the "application/dtmf-relay" body, as defined
  820. // earlier, with the button pressed and the duration it was pressed
  821. // for. Technically, this actually requires the INFO to be generated
  822. // when the user *releases* the button, however if the user has still
  823. // not released a button after 5 seconds, which is the maximum duration
  824. // supported by this mechanism, the UA should generate the INFO at that
  825. // time.
  826. // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00#section-5.3
  827. const dtmf = tone;
  828. const duration = 2000;
  829. const body = {
  830. contentDisposition: 'render',
  831. contentType: 'application/dtmf-relay',
  832. content: 'Signal=' + dtmf + '\r\nDuration=' + duration
  833. };
  834. const requestOptions = {
  835. body
  836. };
  837. return session.info({
  838. requestOptions
  839. }).then(() => {
  840. return;
  841. });
  842. }
  843. });
  844. }
  845. /**
  846. * Transfer.
  847. * @param session - Session with the transferee to transfer.
  848. * @param target - The referral target.
  849. * @remarks
  850. * If target is a Session this is an attended transfer completion (REFER with Replaces),
  851. * otherwise this is a blind transfer (REFER). Attempting an attended transfer
  852. * completion on a call that has not been answered will be rejected. To implement
  853. * an attended transfer with early completion, hangup the call with the target
  854. * and execute a blind transfer to the target.
  855. */
  856. transfer(session, target, options) {
  857. return __awaiter(this, void 0, void 0, function* () {
  858. this.logger.log(`[${session.id}] Referring session...`);
  859. if (target instanceof Session) {
  860. return session.refer(target, options).then(() => {
  861. return;
  862. });
  863. }
  864. const uri = UserAgent.makeURI(target);
  865. if (!uri) {
  866. return Promise.reject(new Error(`Failed to create a valid URI from "${target}"`));
  867. }
  868. return session.refer(uri, options).then(() => {
  869. return;
  870. });
  871. });
  872. }
  873. /**
  874. * Send a message.
  875. * @remarks
  876. * Send a MESSAGE request.
  877. * @param destination - The target destination for the message. A SIP address to send the MESSAGE to.
  878. */
  879. message(destination, message) {
  880. return __awaiter(this, void 0, void 0, function* () {
  881. this.logger.log(`Sending message...`);
  882. const target = UserAgent.makeURI(destination);
  883. if (!target) {
  884. return Promise.reject(new Error(`Failed to create a valid URI from "${destination}"`));
  885. }
  886. return new Messager(this.userAgent, target, message).message();
  887. });
  888. }
  889. /** Media constraints. */
  890. get constraints() {
  891. let constraints = {
  892. audio: true,
  893. video: false
  894. }; // default to audio only calls
  895. if (this.options.media.constraints) {
  896. constraints = Object.assign({}, this.options.media.constraints);
  897. }
  898. return constraints;
  899. }
  900. /**
  901. * Attempt reconnection up to `reconnectionAttempts` times.
  902. * @param reconnectionAttempt - Current attempt number.
  903. */
  904. attemptReconnection(reconnectionAttempt = 1) {
  905. var _a, _b;
  906. const reconnectionAttempts = this.options.reconnectionAttempts;
  907. const reconnectionDelay = this.options.reconnectionDelay;
  908. if (!this.shouldBeConnected) {
  909. this.logger.log(`Should not be connected currently`);
  910. return; // If intentionally disconnected, don't reconnect.
  911. }
  912. if (this.attemptingReconnection) {
  913. this.logger.log(`Reconnection attempt already in progress`);
  914. }
  915. if (reconnectionAttempt > reconnectionAttempts) {
  916. this.logger.log(`Reconnection maximum attempts reached`);
  917. (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectFailed) === null || _b === void 0 ? void 0 : _b.call(_a);
  918. return;
  919. }
  920. if (reconnectionAttempt === 1) {
  921. this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - trying`);
  922. } else {
  923. this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - trying in ${reconnectionDelay} seconds`);
  924. }
  925. this.attemptingReconnection = true;
  926. setTimeout(() => {
  927. if (!this.shouldBeConnected) {
  928. this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - aborted`);
  929. this.attemptingReconnection = false;
  930. return; // If intentionally disconnected, don't reconnect.
  931. }
  932. this.userAgent.reconnect().then(() => {
  933. this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - succeeded`);
  934. this.attemptingReconnection = false;
  935. }).catch(error => {
  936. this.logger.log(`Reconnection attempt ${reconnectionAttempt} of ${reconnectionAttempts} - failed`);
  937. this.logger.error(error.message);
  938. this.attemptingReconnection = false;
  939. this.attemptReconnection(++reconnectionAttempt);
  940. });
  941. }, reconnectionAttempt === 1 ? 0 : reconnectionDelay * 1000);
  942. }
  943. /**
  944. * Register to receive calls.
  945. * @param withoutDelay - If true attempt immediately, otherwise wait `registrationRetryInterval`.
  946. */
  947. attemptRegistration(withoutDelay = false) {
  948. this.logger.log(`Registration attempt ${withoutDelay ? 'without delay' : ''}`);
  949. if (!this.shouldBeRegistered) {
  950. this.logger.log(`Should not be registered currently`);
  951. return Promise.resolve();
  952. }
  953. // It only makes sense to have one attempt in progress at a time.
  954. // Perhaps we shall (or should) try once again.
  955. if (this.registrationAttemptTimeout !== undefined) {
  956. this.logger.log(`Registration attempt already in progress`);
  957. return Promise.resolve();
  958. }
  959. // Helper function to send the register request.
  960. const _register = () => {
  961. // If we do not have a registerer, it is not worth trying to register.
  962. if (!this.registerer) {
  963. this.logger.log(`Registerer undefined`);
  964. return Promise.resolve();
  965. }
  966. // If the WebSocket transport is not connected, it is not worth trying to register.
  967. // Perhpas we shall (or should) try once we are connected.
  968. if (!this.isConnected()) {
  969. this.logger.log(`User agent not connected`);
  970. return Promise.resolve();
  971. }
  972. // If the UserAgent is stopped, it is not worth trying to register.
  973. // Perhaps we shall (or should) try once the UserAgent is running.
  974. if (this.userAgent.state === UserAgentState.Stopped) {
  975. this.logger.log(`User agent stopped`);
  976. return Promise.resolve();
  977. }
  978. // If no guard defined, we are good to proceed without any further ado.
  979. if (!this.options.registerGuard) {
  980. return this.registerer.register(this.registererRegisterOptions).then(() => {
  981. return;
  982. });
  983. }
  984. // Otherwise check to make sure the guard does not want us halt.
  985. return this.options.registerGuard().catch(error => {
  986. this.logger.log(`Register guard rejected will making registration attempt`);
  987. throw error;
  988. }).then(halt => {
  989. if (halt || !this.registerer) {
  990. return Promise.resolve();
  991. }
  992. return this.registerer.register(this.registererRegisterOptions).then(() => {
  993. return;
  994. });
  995. });
  996. };
  997. // Compute an amount of time in seconds to wait before sending another register request.
  998. // This is a small attempt to avoid DOS attacking our own backend in the event that a
  999. // relatively large number of clients sychonously keep retrying register reqeusts.
  1000. // This is known to happen when the backend goes down for a period and all clients
  1001. // are attempting to register again - the backend gets slammed with synced reqeusts.
  1002. const computeRegistrationTimeout = lowerBound => {
  1003. const upperBound = lowerBound * 2;
  1004. return 1000 * (Math.random() * (upperBound - lowerBound) + lowerBound);
  1005. };
  1006. // Send register request after a delay
  1007. return new Promise((resolve, reject) => {
  1008. this.registrationAttemptTimeout = setTimeout(() => {
  1009. _register().then(() => {
  1010. this.registrationAttemptTimeout = undefined;
  1011. resolve();
  1012. }).catch(error => {
  1013. this.registrationAttemptTimeout = undefined;
  1014. if (error instanceof RequestPendingError) {
  1015. resolve();
  1016. } else {
  1017. reject(error);
  1018. }
  1019. });
  1020. }, withoutDelay ? 0 : computeRegistrationTimeout(this.options.registrationRetryInterval));
  1021. });
  1022. }
  1023. /** Helper function to remove media from html elements. */
  1024. cleanupMedia(session) {
  1025. const managedSession = this.sessionManaged(session);
  1026. if (!managedSession) {
  1027. throw new Error('Managed session does not exist.');
  1028. }
  1029. if (managedSession.mediaLocal) {
  1030. if (managedSession.mediaLocal.video) {
  1031. managedSession.mediaLocal.video.srcObject = null;
  1032. managedSession.mediaLocal.video.pause();
  1033. }
  1034. }
  1035. if (managedSession.mediaRemote) {
  1036. if (managedSession.mediaRemote.audio) {
  1037. managedSession.mediaRemote.audio.srcObject = null;
  1038. managedSession.mediaRemote.audio.pause();
  1039. }
  1040. if (managedSession.mediaRemote.video) {
  1041. managedSession.mediaRemote.video.srcObject = null;
  1042. managedSession.mediaRemote.video.pause();
  1043. }
  1044. }
  1045. }
  1046. /** Helper function to enable/disable media tracks. */
  1047. enableReceiverTracks(session, enable) {
  1048. if (!this.sessionExists(session)) {
  1049. throw new Error('Session does not exist.');
  1050. }
  1051. const sessionDescriptionHandler = session.sessionDescriptionHandler;
  1052. if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) {
  1053. throw new Error("Session's session description handler not instance of SessionDescriptionHandler.");
  1054. }
  1055. sessionDescriptionHandler.enableReceiverTracks(enable);
  1056. }
  1057. /** Helper function to enable/disable media tracks. */
  1058. enableSenderTracks(session, enable) {
  1059. if (!this.sessionExists(session)) {
  1060. throw new Error('Session does not exist.');
  1061. }
  1062. const sessionDescriptionHandler = session.sessionDescriptionHandler;
  1063. if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) {
  1064. throw new Error("Session's session description handler not instance of SessionDescriptionHandler.");
  1065. }
  1066. sessionDescriptionHandler.enableSenderTracks(enable);
  1067. }
  1068. /**
  1069. * Setup session delegate and state change handler.
  1070. * @param session - Session to setup.
  1071. * @param referralInviterOptions - Options for any Inviter created as result of a REFER.
  1072. */
  1073. initSession(session, referralInviterOptions) {
  1074. // Add the session
  1075. this.sessionAdd(session);
  1076. // Call session created callback
  1077. if (this.delegate && this.delegate.onCallCreated) {
  1078. this.delegate.onCallCreated(session);
  1079. }
  1080. // Setup session state change handler
  1081. session.stateChange.addListener(state => {
  1082. this.logger.log(`[${session.id}] Session state changed to ${state}`);
  1083. switch (state) {
  1084. case SessionState.Initial:
  1085. break;
  1086. case SessionState.Establishing:
  1087. break;
  1088. case SessionState.Established:
  1089. this.setupLocalMedia(session);
  1090. this.setupRemoteMedia(session);
  1091. if (this.delegate && this.delegate.onCallAnswered) {
  1092. this.delegate.onCallAnswered(session);
  1093. }
  1094. break;
  1095. case SessionState.Terminating:
  1096. // fall through
  1097. case SessionState.Terminated:
  1098. // This will already have executed if/when we fall
  1099. // through from Terminating and thus the managed
  1100. // session may already have been cleaned up.
  1101. if (this.sessionExists(session)) {
  1102. this.cleanupMedia(session);
  1103. this.sessionRemove(session);
  1104. if (this.delegate && this.delegate.onCallHangup) {
  1105. this.delegate.onCallHangup(session);
  1106. }
  1107. }
  1108. break;
  1109. default:
  1110. throw new Error('Unknown session state.');
  1111. }
  1112. });
  1113. // TODO: Any existing onInfo or onRefer delegate gets clobbered here.
  1114. // Setup delegate
  1115. session.delegate = session.delegate || {};
  1116. session.delegate.onInfo = info => {
  1117. // As RFC 6086 states, sending DTMF via INFO is not standardized...
  1118. //
  1119. // Companies have been using INFO messages in order to transport
  1120. // Dual-Tone Multi-Frequency (DTMF) tones. All mechanisms are
  1121. // proprietary and have not been standardized.
  1122. // https://tools.ietf.org/html/rfc6086#section-2
  1123. //
  1124. // It is however widely supported based on this draft:
  1125. // https://tools.ietf.org/html/draft-kaplan-dispatch-info-dtmf-package-00
  1126. var _a;
  1127. // FIXME: TODO: We should reject correctly...
  1128. //
  1129. // If a UA receives an INFO request associated with an Info Package that
  1130. // the UA has not indicated willingness to receive, the UA MUST send a
  1131. // 469 (Bad Info Package) response (see Section 11.6), which contains a
  1132. // Recv-Info header field with Info Packages for which the UA is willing
  1133. // to receive INFO requests.
  1134. // https://tools.ietf.org/html/rfc6086#section-4.2.2
  1135. // No delegate
  1136. if (((_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallDTMFReceived) === undefined) {
  1137. info.reject();
  1138. return;
  1139. }
  1140. // Invalid content type
  1141. const contentType = info.request.getHeader('content-type');
  1142. if (!contentType || !/^application\/dtmf-relay/i.exec(contentType)) {
  1143. info.reject();
  1144. return;
  1145. }
  1146. // Invalid body
  1147. const body = info.request.body.split('\r\n', 2);
  1148. if (body.length !== 2) {
  1149. info.reject();
  1150. return;
  1151. }
  1152. // Invalid tone
  1153. let tone;
  1154. const toneRegExp = /^(Signal\s*?=\s*?)([0-9A-D#*]{1})(\s)?.*/;
  1155. if (body[0] !== undefined && toneRegExp.test(body[0])) {
  1156. tone = body[0].replace(toneRegExp, '$2');
  1157. }
  1158. if (!tone) {
  1159. info.reject();
  1160. return;
  1161. }
  1162. // Invalid duration
  1163. let duration;
  1164. const durationRegExp = /^(Duration\s?=\s?)([0-9]{1,4})(\s)?.*/;
  1165. if (body[1] !== undefined && durationRegExp.test(body[1])) {
  1166. duration = parseInt(body[1].replace(durationRegExp, '$2'), 10);
  1167. }
  1168. if (!duration) {
  1169. info.reject();
  1170. return;
  1171. }
  1172. info.accept().then(() => {
  1173. if (this.delegate && this.delegate.onCallDTMFReceived) {
  1174. if (!tone || !duration) {
  1175. throw new Error('Tone or duration undefined.');
  1176. }
  1177. this.delegate.onCallDTMFReceived(session, tone, duration);
  1178. }
  1179. }).catch(error => {
  1180. this.logger.error(error.message);
  1181. });
  1182. };
  1183. session.delegate.onRefer = referral => {
  1184. referral.accept().then(() => this.sendInvite(referral.makeInviter(referralInviterOptions), referralInviterOptions)).catch(error => {
  1185. this.logger.error(error.message);
  1186. });
  1187. };
  1188. }
  1189. /**
  1190. * Periodically send OPTIONS pings and disconnect when a ping fails.
  1191. * @param requestURI - Request URI to target
  1192. * @param fromURI - From URI
  1193. * @param toURI - To URI
  1194. */
  1195. optionsPingRun(requestURI, fromURI, toURI) {
  1196. // Guard against nvalid interval
  1197. if (this.options.optionsPingInterval < 1) {
  1198. throw new Error('Invalid options ping interval.');
  1199. }
  1200. // Guard against sending a ping when there is one outstanading
  1201. if (this.optionsPingRunning) {
  1202. return;
  1203. }
  1204. this.optionsPingRunning = true;
  1205. // Setup next ping to run in future
  1206. this.optionsPingTimeout = setTimeout(() => {
  1207. this.optionsPingTimeout = undefined;
  1208. // If ping succeeds...
  1209. const onPingSuccess = () => {
  1210. // record success or failure
  1211. this.optionsPingFailure = false;
  1212. // if we are still running, queue up the next ping
  1213. if (this.optionsPingRunning) {
  1214. this.optionsPingRunning = false;
  1215. this.optionsPingRun(requestURI, fromURI, toURI);
  1216. }
  1217. };
  1218. // If ping fails...
  1219. const onPingFailure = () => {
  1220. this.logger.error('OPTIONS ping failed');
  1221. // record success or failure
  1222. this.optionsPingFailure = true;
  1223. // stop running
  1224. this.optionsPingRunning = false;
  1225. // disconnect the transport
  1226. this.userAgent.transport.disconnect().catch(error => this.logger.error(error));
  1227. };
  1228. // Create an OPTIONS request message
  1229. const core = this.userAgent.userAgentCore;
  1230. const message = core.makeOutgoingRequestMessage('OPTIONS', requestURI, fromURI, toURI, {});
  1231. // Send the request message
  1232. this.optionsPingRequest = core.request(message, {
  1233. onAccept: () => {
  1234. this.optionsPingRequest = undefined;
  1235. onPingSuccess();
  1236. },
  1237. onReject: response => {
  1238. this.optionsPingRequest = undefined;
  1239. // Ping fails on following responses...
  1240. // - 408 Request Timeout (no response was received)
  1241. // - 503 Service Unavailable (a transport layer error occured)
  1242. if (response.message.statusCode === 408 || response.message.statusCode === 503) {
  1243. onPingFailure();
  1244. } else {
  1245. onPingSuccess();
  1246. }
  1247. }
  1248. });
  1249. }, this.options.optionsPingInterval * 1000);
  1250. }
  1251. /**
  1252. * Start sending OPTIONS pings.
  1253. */
  1254. optionsPingStart() {
  1255. this.logger.log(`OPTIONS pings started`);
  1256. // Create the URIs needed to send OPTIONS pings
  1257. let requestURI, fromURI, toURI;
  1258. if (this.options.optionsPingRequestURI) {
  1259. // Use whatever specific RURI is provided.
  1260. requestURI = UserAgent.makeURI(this.options.optionsPingRequestURI);
  1261. if (!requestURI) {
  1262. throw new Error('Failed to create Request URI.');
  1263. }
  1264. // Use the user agent's contact URI for From and To URIs
  1265. fromURI = this.userAgent.contact.uri.clone();
  1266. toURI = this.userAgent.contact.uri.clone();
  1267. } else if (this.options.aor) {
  1268. // Otherwise use the AOR provided to target the assocated registrar server.
  1269. const uri = UserAgent.makeURI(this.options.aor);
  1270. if (!uri) {
  1271. throw new Error('Failed to create URI.');
  1272. }
  1273. requestURI = uri.clone();
  1274. requestURI.user = undefined; // target the registrar server
  1275. fromURI = uri.clone();
  1276. toURI = uri.clone();
  1277. } else {
  1278. 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. ');
  1279. return;
  1280. }
  1281. // Send the OPTIONS pings
  1282. this.optionsPingRun(requestURI, fromURI, toURI);
  1283. }
  1284. /**
  1285. * Stop sending OPTIONS pings.
  1286. */
  1287. optionsPingStop() {
  1288. this.logger.log(`OPTIONS pings stopped`);
  1289. this.optionsPingRunning = false;
  1290. this.optionsPingFailure = false;
  1291. if (this.optionsPingRequest) {
  1292. this.optionsPingRequest.dispose();
  1293. this.optionsPingRequest = undefined;
  1294. }
  1295. if (this.optionsPingTimeout) {
  1296. clearTimeout(this.optionsPingTimeout);
  1297. this.optionsPingTimeout = undefined;
  1298. }
  1299. }
  1300. /** Helper function to init send then send invite. */
  1301. sendInvite(inviter, inviterOptions, inviterInviteOptions) {
  1302. return __awaiter(this, void 0, void 0, function* () {
  1303. // Initialize our session
  1304. this.initSession(inviter, inviterOptions);
  1305. // Send the INVITE
  1306. return inviter.invite(inviterInviteOptions).then(() => {
  1307. this.logger.log(`[${inviter.id}] Sent INVITE`);
  1308. });
  1309. });
  1310. }
  1311. managedSessionFactory(_sessionManagerPlus, session) {
  1312. return {
  1313. session,
  1314. held: false,
  1315. muted: false
  1316. };
  1317. }
  1318. /** Helper function to add a session to the ones we are managing. */
  1319. sessionAdd(session) {
  1320. const managedSession = this.managedSessionFactory(this, session);
  1321. this.managedSessions.push(managedSession);
  1322. }
  1323. /** Helper function to check if the session is one we are managing. */
  1324. sessionExists(session) {
  1325. return this.sessionManaged(session) !== undefined;
  1326. }
  1327. /** Helper function to check if the session is one we are managing. */
  1328. sessionManaged(session) {
  1329. return this.managedSessions.find(el => el.session.id === session.id);
  1330. }
  1331. /** Helper function to remoce a session from the ones we are managing. */
  1332. sessionRemove(session) {
  1333. this.managedSessions = this.managedSessions.filter(el => el.session.id !== session.id);
  1334. }
  1335. /**
  1336. * Puts Session on hold.
  1337. * @param session - The session to set.
  1338. * @param hold - Hold on if true, off if false.
  1339. */
  1340. setHold(session, hold) {
  1341. return __awaiter(this, void 0, void 0, function* () {
  1342. if (!this.sessionExists(session)) {
  1343. return Promise.reject(new Error('Session does not exist.'));
  1344. }
  1345. // Just resolve if we are already in correct state
  1346. if (this.isHeld(session) === hold) {
  1347. return Promise.resolve();
  1348. }
  1349. const sessionDescriptionHandler = session.sessionDescriptionHandler;
  1350. if (!(sessionDescriptionHandler instanceof Web.SessionDescriptionHandler)) {
  1351. throw new Error("Session's session description handler not instance of SessionDescriptionHandler.");
  1352. }
  1353. const options = {
  1354. requestDelegate: {
  1355. onAccept: () => {
  1356. const managedSession = this.sessionManaged(session);
  1357. if (managedSession !== undefined) {
  1358. managedSession.held = hold;
  1359. this.enableReceiverTracks(session, !managedSession.held);
  1360. this.enableSenderTracks(session, !managedSession.held && !managedSession.muted);
  1361. if (this.delegate && this.delegate.onCallHold) {
  1362. this.delegate.onCallHold(session, managedSession.held);
  1363. }
  1364. }
  1365. },
  1366. onReject: () => {
  1367. this.logger.warn(`[${session.id}] Re-invite request was rejected`);
  1368. const managedSession = this.sessionManaged(session);
  1369. if (managedSession !== undefined) {
  1370. managedSession.held = !hold; // this was preemptively set so undo on failure
  1371. this.enableReceiverTracks(session, !managedSession.held);
  1372. this.enableSenderTracks(session, !managedSession.held && !managedSession.muted);
  1373. if (this.delegate && this.delegate.onCallHold) {
  1374. this.delegate.onCallHold(session, managedSession.held);
  1375. }
  1376. }
  1377. }
  1378. }
  1379. };
  1380. // Session properties used to pass options to the SessionDescriptionHandler:
  1381. //
  1382. // 1) Session.sessionDescriptionHandlerOptions
  1383. // hsH options for the initial INVITE transaction.
  1384. // - Used in all cases when handling the initial INVITE transaction as either UAC or UAS.
  1385. // - May be set directly at anytime.
  1386. // - May optionally be set via constructor option.
  1387. // - May optionally be set via options passed to Inviter.invite() or Invitation.accept().
  1388. //
  1389. // 2) Session.sessionDescriptionHandlerOptionsReInvite
  1390. // hsH options for re-INVITE transactions.
  1391. // - Used in all cases when handling a re-INVITE transaction as either UAC or UAS.
  1392. // - May be set directly at anytime.
  1393. // - May optionally be set via constructor option.
  1394. // - May optionally be set via options passed to Session.invite().
  1395. const sessionDescriptionHandlerOptions = session.sessionDescriptionHandlerOptionsReInvite;
  1396. sessionDescriptionHandlerOptions.hold = hold;
  1397. session.sessionDescriptionHandlerOptionsReInvite = sessionDescriptionHandlerOptions;
  1398. // Preemptively and optimistically set held state (but do not call delegate).
  1399. const managedSession = this.sessionManaged(session);
  1400. if (!managedSession) {
  1401. throw new Error('Managed session is undefiend.');
  1402. }
  1403. managedSession.held = hold;
  1404. // Send re-INVITE
  1405. return session.invite(options).then(() => {
  1406. // Preemptively enable/disable tracks
  1407. const managedSession = this.sessionManaged(session);
  1408. if (managedSession !== undefined) {
  1409. this.enableReceiverTracks(session, !managedSession.held);
  1410. this.enableSenderTracks(session, !managedSession.held && !managedSession.muted);
  1411. }
  1412. }).catch(error => {
  1413. managedSession.held = !hold; // was preemptively set so undo on failure
  1414. if (error instanceof RequestPendingError) {
  1415. this.logger.error(`[${session.id}] A hold request is already in progress.`);
  1416. }
  1417. throw error;
  1418. });
  1419. });
  1420. }
  1421. /**
  1422. * Puts Session on mute.
  1423. * @param session - The session to mute.
  1424. * @param mute - Mute on if true, off if false.
  1425. */
  1426. setMute(session, mute) {
  1427. if (!this.sessionExists(session)) {
  1428. this.logger.warn(`[${session.id}] A session is required to enabled/disable media tracks`);
  1429. return;
  1430. }
  1431. if (session.state !== SessionState.Established) {
  1432. this.logger.warn(`[${session.id}] An established session is required to enable/disable media tracks`);
  1433. return;
  1434. }
  1435. const managedSession = this.sessionManaged(session);
  1436. if (managedSession !== undefined) {
  1437. managedSession.muted = mute;
  1438. this.enableSenderTracks(session, !managedSession.held && !managedSession.muted);
  1439. }
  1440. }
  1441. /** Helper function to attach local media to html elements. */
  1442. setupLocalMedia(session) {
  1443. const managedSession = this.sessionManaged(session);
  1444. if (!managedSession) {
  1445. throw new Error('Managed session does not exist.');
  1446. }
  1447. // Get the local media element, if any, from the and configuraiton options
  1448. // and save the info with the managed session so we can clean it up later.
  1449. const mediaLocal = typeof this.options.media.local === 'function' ? this.options.media.local(session) : this.options.media.local;
  1450. managedSession.mediaLocal = mediaLocal;
  1451. const mediaElement = mediaLocal === null || mediaLocal === void 0 ? void 0 : mediaLocal.video;
  1452. if (mediaElement) {
  1453. const localStream = this.getLocalMediaStream(session);
  1454. if (!localStream) {
  1455. throw new Error('Local media stream undefiend.');
  1456. }
  1457. mediaElement.srcObject = localStream;
  1458. mediaElement.volume = 0;
  1459. mediaElement.play().catch(error => {
  1460. this.logger.error(`[${session.id}] Failed to play local media`);
  1461. this.logger.error(error.message);
  1462. });
  1463. }
  1464. }
  1465. /** Helper function to attach remote media to html elements. */
  1466. setupRemoteMedia(session) {
  1467. const managedSession = this.sessionManaged(session);
  1468. if (!managedSession) {
  1469. throw new Error('Managed session does not exist.');
  1470. }
  1471. // Get the remote media element, if any, from the and configuraiton options
  1472. // and save the info with the managed session so we can clean it up later.
  1473. const mediaRemote = typeof this.options.media.remote === 'function' ? this.options.media.remote(session) : this.options.media.remote;
  1474. managedSession.mediaRemote = mediaRemote;
  1475. const mediaElement = (mediaRemote === null || mediaRemote === void 0 ? void 0 : mediaRemote.video) || (mediaRemote === null || mediaRemote === void 0 ? void 0 : mediaRemote.audio);
  1476. if (mediaElement) {
  1477. const remoteStream = this.getRemoteMediaStream(session);
  1478. if (!remoteStream) {
  1479. throw new Error('Remote media stream undefiend.');
  1480. }
  1481. mediaElement.autoplay = true; // Safari hack, because you cannot call .play() from a non user action
  1482. mediaElement.srcObject = remoteStream;
  1483. mediaElement.play().catch(error => {
  1484. this.logger.error(`[${session.id}] Failed to play remote media`);
  1485. this.logger.error(error.message);
  1486. });
  1487. remoteStream.onaddtrack = () => {
  1488. this.logger.log(`Remote media onaddtrack`);
  1489. mediaElement.load(); // Safari hack, as it doesn't work otheriwse
  1490. mediaElement.play().catch(error => {
  1491. this.logger.error(`[${session.id}] Failed to play remote media`);
  1492. this.logger.error(error.message);
  1493. });
  1494. };
  1495. }
  1496. }
  1497. /**
  1498. * End a session.
  1499. * @param session - The session to terminate.
  1500. * @remarks
  1501. * Send a BYE request, CANCEL request or reject response to end the current Session.
  1502. * Resolves when the request/response is sent, otherwise rejects.
  1503. * Use `onCallHangup` delegate method to determine if and when Session is terminated.
  1504. */
  1505. terminate(session) {
  1506. return __awaiter(this, void 0, void 0, function* () {
  1507. this.logger.log(`[${session.id}] Terminating...`);
  1508. switch (session.state) {
  1509. case SessionState.Initial:
  1510. if (session instanceof Inviter) {
  1511. return session.cancel().then(() => {
  1512. this.logger.log(`[${session.id}] Inviter never sent INVITE (canceled)`);
  1513. });
  1514. } else if (session instanceof Invitation) {
  1515. return session.reject().then(() => {
  1516. this.logger.log(`[${session.id}] Invitation rejected (sent 480)`);
  1517. });
  1518. } else {
  1519. throw new Error('Unknown session type.');
  1520. }
  1521. case SessionState.Establishing:
  1522. if (session instanceof Inviter) {
  1523. return session.cancel().then(() => {
  1524. this.logger.log(`[${session.id}] Inviter canceled (sent CANCEL)`);
  1525. });
  1526. } else if (session instanceof Invitation) {
  1527. return session.reject().then(() => {
  1528. this.logger.log(`[${session.id}] Invitation rejected (sent 480)`);
  1529. });
  1530. } else {
  1531. throw new Error('Unknown session type.');
  1532. }
  1533. case SessionState.Established:
  1534. return session.bye().then(() => {
  1535. this.logger.log(`[${session.id}] Session ended (sent BYE)`);
  1536. });
  1537. case SessionState.Terminating:
  1538. break;
  1539. case SessionState.Terminated:
  1540. break;
  1541. default:
  1542. throw new Error('Unknown state');
  1543. }
  1544. this.logger.log(`[${session.id}] Terminating in state ${session.state}, no action taken`);
  1545. return Promise.resolve();
  1546. });
  1547. }
  1548. }
  1549. /**
  1550. * A simple SIP user class.
  1551. * @remarks
  1552. * While this class is completely functional for simple use cases, it is not intended
  1553. * to provide an interface which is suitable for most (must less all) applications.
  1554. * While this class has many limitations (for example, it only handles a single concurrent session),
  1555. * it is, however, intended to serve as a simple example of using the SIP.js API.
  1556. * @public
  1557. */
  1558. class SimpleUserPlus {
  1559. /**
  1560. * Constructs a new instance of the `SimpleUser` class.
  1561. * @param server - SIP WebSocket Server URL.
  1562. * @param options - Options bucket. See {@link SimpleUserOptions} for details.
  1563. */
  1564. constructor(server, options = {}) {
  1565. /** Delegate. */
  1566. Object.defineProperty(this, "delegate", {
  1567. enumerable: true,
  1568. configurable: true,
  1569. writable: true,
  1570. value: void 0
  1571. });
  1572. Object.defineProperty(this, "logger", {
  1573. enumerable: true,
  1574. configurable: true,
  1575. writable: true,
  1576. value: void 0
  1577. });
  1578. Object.defineProperty(this, "options", {
  1579. enumerable: true,
  1580. configurable: true,
  1581. writable: true,
  1582. value: void 0
  1583. });
  1584. Object.defineProperty(this, "session", {
  1585. enumerable: true,
  1586. configurable: true,
  1587. writable: true,
  1588. value: undefined
  1589. });
  1590. Object.defineProperty(this, "sessionManager", {
  1591. enumerable: true,
  1592. configurable: true,
  1593. writable: true,
  1594. value: void 0
  1595. });
  1596. // Delegate
  1597. this.delegate = options.delegate;
  1598. // Copy options
  1599. this.options = Object.assign({}, options);
  1600. // Session manager options
  1601. const sessionManagerOptions = {
  1602. aor: this.options.aor,
  1603. delegate: {
  1604. onCallAnswered: () => {
  1605. var _a, _b;
  1606. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallAnswered) === null || _b === void 0 ? void 0 : _b.call(_a);
  1607. },
  1608. onCallCreated: session => {
  1609. var _a, _b, _c, _d;
  1610. this.session = session;
  1611. (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallBegin) === null || _b === void 0 ? void 0 : _b.call(_a, session);
  1612. (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onCallCreated) === null || _d === void 0 ? void 0 : _d.call(_c);
  1613. },
  1614. onCallReceived: session => {
  1615. var _a, _b, _c, _d;
  1616. (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallReceived) === null || _b === void 0 ? void 0 : _b.call(_a);
  1617. (_d = (_c = this.delegate) === null || _c === void 0 ? void 0 : _c.onInvite) === null || _d === void 0 ? void 0 : _d.call(_c, session);
  1618. },
  1619. onCallHangup: () => {
  1620. var _a, _b;
  1621. this.session = undefined;
  1622. ((_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallHangup) && ((_b = this.delegate) === null || _b === void 0 ? void 0 : _b.onCallHangup());
  1623. },
  1624. onCallHold: (_s, held) => {
  1625. var _a, _b;
  1626. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallHold) === null || _b === void 0 ? void 0 : _b.call(_a, held);
  1627. },
  1628. onCallDTMFReceived: (_s, tone, dur) => {
  1629. var _a, _b;
  1630. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onCallDTMFReceived) === null || _b === void 0 ? void 0 : _b.call(_a, tone, dur);
  1631. },
  1632. onMessageReceived: message => {
  1633. var _a, _b;
  1634. 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);
  1635. },
  1636. onRegistered: () => {
  1637. var _a, _b;
  1638. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onRegistered) === null || _b === void 0 ? void 0 : _b.call(_a);
  1639. },
  1640. onUnregistered: () => {
  1641. var _a, _b;
  1642. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onUnregistered) === null || _b === void 0 ? void 0 : _b.call(_a);
  1643. },
  1644. onServerConnect: () => {
  1645. var _a, _b;
  1646. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onServerConnect) === null || _b === void 0 ? void 0 : _b.call(_a);
  1647. },
  1648. onServerDisconnect: () => {
  1649. var _a, _b;
  1650. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onServerDisconnect) === null || _b === void 0 ? void 0 : _b.call(_a);
  1651. },
  1652. onReconnectFailed: () => {
  1653. var _a, _b;
  1654. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectFailed) === null || _b === void 0 ? void 0 : _b.call(_a);
  1655. },
  1656. onReconnectStart: () => {
  1657. var _a, _b;
  1658. return (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onReconnectStart) === null || _b === void 0 ? void 0 : _b.call(_a);
  1659. }
  1660. },
  1661. maxSimultaneousSessions: 1,
  1662. media: this.options.media,
  1663. optionsPingInterval: this.options.optionsPingInterval,
  1664. optionsPingRequestURI: this.options.aor,
  1665. reconnectionAttempts: this.options.reconnectionAttempts,
  1666. reconnectionDelay: this.options.reconnectionDelay,
  1667. registererOptions: this.options.registererOptions,
  1668. sendDTMFUsingSessionDescriptionHandler: this.options.sendDTMFUsingSessionDescriptionHandler,
  1669. userAgentOptions: this.options.userAgentOptions
  1670. };
  1671. this.sessionManager = new SessionManagerPlus(server, sessionManagerOptions);
  1672. this.sessionManager.userAgent.stateChange.addListener(state => {
  1673. var _a, _b;
  1674. (_b = (_a = this.delegate) === null || _a === void 0 ? void 0 : _a.onUserAgentStateChange) === null || _b === void 0 ? void 0 : _b.call(_a, state);
  1675. });
  1676. // Use the SIP.js logger
  1677. this.logger = this.sessionManager.userAgent.getLogger('sip.SimpleUser');
  1678. }
  1679. /**
  1680. * Instance identifier.
  1681. * @internal
  1682. */
  1683. get id() {
  1684. return this.options.userAgentOptions && this.options.userAgentOptions.displayName || 'Anonymous';
  1685. }
  1686. /** The local media stream. Undefined if call not answered. */
  1687. get localMediaStream() {
  1688. return this.session && this.sessionManager.getLocalMediaStream(this.session);
  1689. }
  1690. /** The remote media stream. Undefined if call not answered. */
  1691. get remoteMediaStream() {
  1692. return this.session && this.sessionManager.getRemoteMediaStream(this.session);
  1693. }
  1694. /**
  1695. * The local audio track, if available.
  1696. * @deprecated Use localMediaStream and get track from the stream.
  1697. */
  1698. get localAudioTrack() {
  1699. return this.session && this.sessionManager.getLocalAudioTrack(this.session);
  1700. }
  1701. /**
  1702. * The local video track, if available.
  1703. * @deprecated Use localMediaStream and get track from the stream.
  1704. */
  1705. get localVideoTrack() {
  1706. return this.session && this.sessionManager.getLocalVideoTrack(this.session);
  1707. }
  1708. /**
  1709. * The remote audio track, if available.
  1710. * @deprecated Use remoteMediaStream and get track from the stream.
  1711. */
  1712. get remoteAudioTrack() {
  1713. return this.session && this.sessionManager.getRemoteAudioTrack(this.session);
  1714. }
  1715. /**
  1716. * The remote video track, if available.
  1717. * @deprecated Use remoteMediaStream and get track from the stream.
  1718. */
  1719. get remoteVideoTrack() {
  1720. return this.session && this.sessionManager.getRemoteVideoTrack(this.session);
  1721. }
  1722. /**
  1723. * Connect.
  1724. * @remarks
  1725. * Start the UserAgent's WebSocket Transport.
  1726. */
  1727. connect() {
  1728. this.logger.log(`[${this.id}] Connecting UserAgent...`);
  1729. return this.sessionManager.connect();
  1730. }
  1731. /**
  1732. * Disconnect.
  1733. * @remarks
  1734. * Stop the UserAgent's WebSocket Transport.
  1735. */
  1736. disconnect() {
  1737. this.logger.log(`[${this.id}] Disconnecting UserAgent...`);
  1738. return this.sessionManager.disconnect();
  1739. }
  1740. /**
  1741. * Return true if connected.
  1742. */
  1743. isConnected() {
  1744. return this.sessionManager.isConnected();
  1745. }
  1746. /**
  1747. * Start receiving incoming calls.
  1748. * @remarks
  1749. * Send a REGISTER request for the UserAgent's AOR.
  1750. * Resolves when the REGISTER request is sent, otherwise rejects.
  1751. */
  1752. register(registererRegisterOptions) {
  1753. this.logger.log(`[${this.id}] Registering UserAgent...`);
  1754. return this.sessionManager.register(registererRegisterOptions);
  1755. }
  1756. /**
  1757. * Stop receiving incoming calls.
  1758. * @remarks
  1759. * Send an un-REGISTER request for the UserAgent's AOR.
  1760. * Resolves when the un-REGISTER request is sent, otherwise rejects.
  1761. */
  1762. unregister(registererUnregisterOptions) {
  1763. this.logger.log(`[${this.id}] Unregistering UserAgent...`);
  1764. return this.sessionManager.unregister(registererUnregisterOptions);
  1765. }
  1766. /**
  1767. * Make an outgoing call.
  1768. * @remarks
  1769. * Send an INVITE request to create a new Session.
  1770. * Resolves when the INVITE request is sent, otherwise rejects.
  1771. * Use `onCallAnswered` delegate method to determine if Session is established.
  1772. * @param destination - The target destination to call. A SIP address to send the INVITE to.
  1773. * @param inviterOptions - Optional options for Inviter constructor.
  1774. * @param inviterInviteOptions - Optional options for Inviter.invite().
  1775. */
  1776. call(destination, inviterOptions, inviterInviteOptions) {
  1777. this.logger.log(`[${this.id}] Beginning Session...`);
  1778. if (this.session) {
  1779. return Promise.reject(new Error('Session already exists.'));
  1780. }
  1781. return this.sessionManager.call(destination, inviterOptions, inviterInviteOptions).then(() => {
  1782. return;
  1783. });
  1784. }
  1785. /**
  1786. * Hangup a call.
  1787. * @remarks
  1788. * Send a BYE request, CANCEL request or reject response to end the current Session.
  1789. * Resolves when the request/response is sent, otherwise rejects.
  1790. * Use `onCallHangup` delegate method to determine if and when call is ended.
  1791. */
  1792. hangup() {
  1793. this.logger.log(`[${this.id}] Hangup...`);
  1794. if (!this.session) {
  1795. return Promise.reject(new Error('Session does not exist.'));
  1796. }
  1797. return this.sessionManager.hangup(this.session).then(() => {
  1798. this.session = undefined;
  1799. });
  1800. }
  1801. /**
  1802. * Answer an incoming call.
  1803. * @remarks
  1804. * Accept an incoming INVITE request creating a new Session.
  1805. * Resolves with the response is sent, otherwise rejects.
  1806. * Use `onCallAnswered` delegate method to determine if and when call is established.
  1807. * @param invitationAcceptOptions - Optional options for Inviter.accept().
  1808. */
  1809. answer(invitationAcceptOptions) {
  1810. this.logger.log(`[${this.id}] Accepting Invitation...`);
  1811. if (!this.session) {
  1812. return Promise.reject(new Error('Session does not exist.'));
  1813. }
  1814. return this.sessionManager.answer(this.session, invitationAcceptOptions);
  1815. }
  1816. /**
  1817. * Decline an incoming call.
  1818. * @remarks
  1819. * Reject an incoming INVITE request.
  1820. * Resolves with the response is sent, otherwise rejects.
  1821. * Use `onCallHangup` delegate method to determine if and when call is ended.
  1822. */
  1823. decline() {
  1824. this.logger.log(`[${this.id}] rejecting Invitation...`);
  1825. if (!this.session) {
  1826. return Promise.reject(new Error('Session does not exist.'));
  1827. }
  1828. return this.sessionManager.decline(this.session);
  1829. }
  1830. /**
  1831. * Hold call
  1832. * @remarks
  1833. * Send a re-INVITE with new offer indicating "hold".
  1834. * Resolves when the re-INVITE request is sent, otherwise rejects.
  1835. * Use `onCallHold` delegate method to determine if request is accepted or rejected.
  1836. * See: https://tools.ietf.org/html/rfc6337
  1837. */
  1838. hold() {
  1839. this.logger.log(`[${this.id}] holding session...`);
  1840. if (!this.session) {
  1841. return Promise.reject(new Error('Session does not exist.'));
  1842. }
  1843. return this.sessionManager.hold(this.session);
  1844. }
  1845. /**
  1846. * Unhold call.
  1847. * @remarks
  1848. * Send a re-INVITE with new offer indicating "unhold".
  1849. * Resolves when the re-INVITE request is sent, otherwise rejects.
  1850. * Use `onCallHold` delegate method to determine if request is accepted or rejected.
  1851. * See: https://tools.ietf.org/html/rfc6337
  1852. */
  1853. unhold() {
  1854. this.logger.log(`[${this.id}] unholding session...`);
  1855. if (!this.session) {
  1856. return Promise.reject(new Error('Session does not exist.'));
  1857. }
  1858. return this.sessionManager.unhold(this.session);
  1859. }
  1860. /**
  1861. * Hold state.
  1862. * @remarks
  1863. * True if session is on hold.
  1864. */
  1865. isHeld() {
  1866. return this.session ? this.sessionManager.isHeld(this.session) : false;
  1867. }
  1868. /**
  1869. * Mute call.
  1870. * @remarks
  1871. * Disable sender's media tracks.
  1872. */
  1873. mute() {
  1874. this.logger.log(`[${this.id}] disabling media tracks...`);
  1875. return this.session && this.sessionManager.mute(this.session);
  1876. }
  1877. /**
  1878. * Unmute call.
  1879. * @remarks
  1880. * Enable sender's media tracks.
  1881. */
  1882. unmute() {
  1883. this.logger.log(`[${this.id}] enabling media tracks...`);
  1884. return this.session && this.sessionManager.unmute(this.session);
  1885. }
  1886. /**
  1887. * Mute state.
  1888. * @remarks
  1889. * True if sender's media track is disabled.
  1890. */
  1891. isMuted() {
  1892. return this.session ? this.sessionManager.isMuted(this.session) : false;
  1893. }
  1894. /**
  1895. * Send DTMF.
  1896. * @remarks
  1897. * Send an INFO request with content type application/dtmf-relay.
  1898. * @param tone - Tone to send.
  1899. */
  1900. sendDTMF(tone) {
  1901. this.logger.log(`[${this.id}] sending DTMF...`);
  1902. if (!this.session) {
  1903. return Promise.reject(new Error('Session does not exist.'));
  1904. }
  1905. return this.sessionManager.sendDTMF(this.session, tone);
  1906. }
  1907. /**
  1908. * Send a message.
  1909. * @remarks
  1910. * Send a MESSAGE request.
  1911. * @param destination - The target destination for the message. A SIP address to send the MESSAGE to.
  1912. */
  1913. message(destination, message) {
  1914. this.logger.log(`[${this.id}] sending message...`);
  1915. return this.sessionManager.message(destination, message);
  1916. }
  1917. }
  1918. let has = Object.prototype.hasOwnProperty,
  1919. prefix = '~';
  1920. /**
  1921. * Constructor to create a storage for our `EE` objects.
  1922. * An `Events` instance is a plain object whose properties are event names.
  1923. *
  1924. * @constructor
  1925. * @private
  1926. */
  1927. // eslint-disable-next-line @typescript-eslint/no-empty-function
  1928. function Events() {}
  1929. //
  1930. // We try to not inherit from `Object.prototype`. In some engines creating an
  1931. // instance in this way is faster than calling `Object.create(null)` directly.
  1932. // If `Object.create(null)` is not supported we prefix the event names with a
  1933. // character to make sure that the built-in object properties are not
  1934. // overridden or used as an attack vector.
  1935. //
  1936. if (Object.create) {
  1937. Events.prototype = Object.create(null);
  1938. //
  1939. // This hack is needed because the `__proto__` property is still inherited in
  1940. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  1941. //
  1942. // eslint-disable-next-line no-const-assign
  1943. if (!new Events().__proto__) prefix = false;
  1944. }
  1945. /**
  1946. * Representation of a single event listener.
  1947. *
  1948. * @param {Function} fn The listener function.
  1949. * @param {*} context The context to invoke the listener with.
  1950. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  1951. * @constructor
  1952. * @private
  1953. */
  1954. function EE(fn, context, once) {
  1955. this.fn = fn;
  1956. this.context = context;
  1957. this.once = once || false;
  1958. }
  1959. /**
  1960. * Add a listener for a given event.
  1961. *
  1962. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  1963. * @param {(String|Symbol)} event The event name.
  1964. * @param {Function} fn The listener function.
  1965. * @param {*} context The context to invoke the listener with.
  1966. * @param {Boolean} once Specify if the listener is a one-time listener.
  1967. * @returns {EventEmitter}
  1968. * @private
  1969. */
  1970. function addListener(emitter, event, fn, context, once) {
  1971. if (typeof fn !== 'function') {
  1972. throw new TypeError('The listener must be a function');
  1973. }
  1974. const listener = new EE(fn, context || emitter, once),
  1975. evt = prefix ? prefix + event : event;
  1976. 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];
  1977. return emitter;
  1978. }
  1979. /**
  1980. * Clear event by name.
  1981. *
  1982. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  1983. * @param {(String|Symbol)} evt The Event name.
  1984. * @private
  1985. */
  1986. function clearEvent(emitter, evt) {
  1987. if (--emitter._eventsCount === 0) emitter._events = new Events();else delete emitter._events[evt];
  1988. }
  1989. /**
  1990. * Minimal `EventEmitter` interface that is molded against the Node.js
  1991. * `EventEmitter` interface.
  1992. *
  1993. * @constructor
  1994. * @public
  1995. */
  1996. function EventEmitter() {
  1997. this._events = new Events();
  1998. this._eventsCount = 0;
  1999. }
  2000. /**
  2001. * Return an array listing the events for which the emitter has registered
  2002. * listeners.
  2003. *
  2004. * @returns {Array}
  2005. * @public
  2006. */
  2007. EventEmitter.prototype.eventNames = function eventNames() {
  2008. let names = [],
  2009. events,
  2010. name;
  2011. if (this._eventsCount === 0) return names;
  2012. for (name in events = this._events) {
  2013. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  2014. }
  2015. if (Object.getOwnPropertySymbols) {
  2016. return names.concat(Object.getOwnPropertySymbols(events));
  2017. }
  2018. return names;
  2019. };
  2020. /**
  2021. * Return the listeners registered for a given event.
  2022. *
  2023. * @param {(String|Symbol)} event The event name.
  2024. * @returns {Array} The registered listeners.
  2025. * @public
  2026. */
  2027. EventEmitter.prototype.listeners = function listeners(event) {
  2028. const evt = prefix ? prefix + event : event,
  2029. handlers = this._events[evt];
  2030. if (!handlers) return [];
  2031. if (handlers.fn) return [handlers.fn];
  2032. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  2033. ee[i] = handlers[i].fn;
  2034. }
  2035. return ee;
  2036. };
  2037. /**
  2038. * Return the number of listeners listening to a given event.
  2039. *
  2040. * @param {(String|Symbol)} event The event name.
  2041. * @returns {Number} The number of listeners.
  2042. * @public
  2043. */
  2044. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  2045. const evt = prefix ? prefix + event : event,
  2046. listeners = this._events[evt];
  2047. if (!listeners) return 0;
  2048. if (listeners.fn) return 1;
  2049. return listeners.length;
  2050. };
  2051. /**
  2052. * Calls each of the listeners registered for a given event.
  2053. *
  2054. * @param {(String|Symbol)} event The event name.
  2055. * @returns {Boolean} `true` if the event had listeners, else `false`.
  2056. * @public
  2057. */
  2058. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  2059. const evt = prefix ? prefix + event : event;
  2060. if (!this._events[evt]) return false;
  2061. let listeners = this._events[evt],
  2062. len = arguments.length,
  2063. args,
  2064. i;
  2065. if (listeners.fn) {
  2066. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  2067. switch (len) {
  2068. case 1:
  2069. return listeners.fn.call(listeners.context), true;
  2070. case 2:
  2071. return listeners.fn.call(listeners.context, a1), true;
  2072. case 3:
  2073. return listeners.fn.call(listeners.context, a1, a2), true;
  2074. case 4:
  2075. return listeners.fn.call(listeners.context, a1, a2, a3), true;
  2076. case 5:
  2077. return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  2078. case 6:
  2079. return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  2080. }
  2081. for (i = 1, args = new Array(len - 1); i < len; i++) {
  2082. args[i - 1] = arguments[i];
  2083. }
  2084. listeners.fn.apply(listeners.context, args);
  2085. } else {
  2086. let length = listeners.length,
  2087. j;
  2088. for (i = 0; i < length; i++) {
  2089. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  2090. switch (len) {
  2091. case 1:
  2092. listeners[i].fn.call(listeners[i].context);
  2093. break;
  2094. case 2:
  2095. listeners[i].fn.call(listeners[i].context, a1);
  2096. break;
  2097. case 3:
  2098. listeners[i].fn.call(listeners[i].context, a1, a2);
  2099. break;
  2100. case 4:
  2101. listeners[i].fn.call(listeners[i].context, a1, a2, a3);
  2102. break;
  2103. default:
  2104. if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) {
  2105. args[j - 1] = arguments[j];
  2106. }
  2107. listeners[i].fn.apply(listeners[i].context, args);
  2108. }
  2109. }
  2110. }
  2111. return true;
  2112. };
  2113. /**
  2114. * Add a listener for a given event.
  2115. *
  2116. * @param {(String|Symbol)} event The event name.
  2117. * @param {Function} fn The listener function.
  2118. * @param {*} [context=this] The context to invoke the listener with.
  2119. * @returns {EventEmitter} `this`.
  2120. * @public
  2121. */
  2122. EventEmitter.prototype.on = function on(event, fn, context) {
  2123. return addListener(this, event, fn, context, false);
  2124. };
  2125. /**
  2126. * Add a one-time listener for a given event.
  2127. *
  2128. * @param {(String|Symbol)} event The event name.
  2129. * @param {Function} fn The listener function.
  2130. * @param {*} [context=this] The context to invoke the listener with.
  2131. * @returns {EventEmitter} `this`.
  2132. * @public
  2133. */
  2134. EventEmitter.prototype.once = function once(event, fn, context) {
  2135. return addListener(this, event, fn, context, true);
  2136. };
  2137. /**
  2138. * Remove the listeners of a given event.
  2139. *
  2140. * @param {(String|Symbol)} event The event name.
  2141. * @param {Function} fn Only remove the listeners that match this function.
  2142. * @param {*} context Only remove the listeners that have this context.
  2143. * @param {Boolean} once Only remove one-time listeners.
  2144. * @returns {EventEmitter} `this`.
  2145. * @public
  2146. */
  2147. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  2148. const evt = prefix ? prefix + event : event;
  2149. if (!this._events[evt]) return this;
  2150. if (!fn) {
  2151. clearEvent(this, evt);
  2152. return this;
  2153. }
  2154. const listeners = this._events[evt];
  2155. if (listeners.fn) {
  2156. if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
  2157. clearEvent(this, evt);
  2158. }
  2159. } else {
  2160. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  2161. if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
  2162. events.push(listeners[i]);
  2163. }
  2164. }
  2165. //
  2166. // Reset the array, or remove it completely if we have no more listeners.
  2167. //
  2168. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;else clearEvent(this, evt);
  2169. }
  2170. return this;
  2171. };
  2172. /**
  2173. * Remove all listeners, or those of the specified event.
  2174. *
  2175. * @param {(String|Symbol)} [event] The event name.
  2176. * @returns {EventEmitter} `this`.
  2177. * @public
  2178. */
  2179. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  2180. let evt;
  2181. if (event) {
  2182. evt = prefix ? prefix + event : event;
  2183. if (this._events[evt]) clearEvent(this, evt);
  2184. } else {
  2185. this._events = new Events();
  2186. this._eventsCount = 0;
  2187. }
  2188. return this;
  2189. };
  2190. //
  2191. // Alias methods names because people roll like that.
  2192. //
  2193. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  2194. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  2195. //
  2196. // Expose the prefix.
  2197. //
  2198. EventEmitter.prefixed = prefix;
  2199. //
  2200. // Allow `EventEmitter` to be imported as module namespace.
  2201. //
  2202. EventEmitter.EventEmitter = EventEmitter;
  2203. //
  2204. // Expose the module.
  2205. //
  2206. if ('undefined' !== typeof module) {
  2207. module.exports = EventEmitter;
  2208. }
  2209. /** 将小驼峰式字符串转换为小写下划线格式
  2210. * @param {string}
  2211. * @returns {string}
  2212. */
  2213. function upperCamelToLowerSnake(s) {
  2214. s = s.replace(/\s/g, '');
  2215. s = s.replace(/([A-Z])/g, '_$1');
  2216. s = s.toLowerCase();
  2217. s = s.replace(/^_/, '');
  2218. s = s.replace(/_$/, '');
  2219. return s;
  2220. }
  2221. /**
  2222. * 获取时间戳字符串
  2223. * @returns {string} 年-月-日 时:分:秒.毫秒
  2224. */
  2225. const getDateTime = () => {
  2226. const timestamp = Date.now();
  2227. const date = new Date(timestamp);
  2228. const year = date.getFullYear();
  2229. const month = String(date.getMonth() + 1).padStart(2, '0');
  2230. const day = String(date.getDate()).padStart(2, '0');
  2231. const hours = String(date.getHours()).padStart(2, '0');
  2232. const minutes = String(date.getMinutes()).padStart(2, '0');
  2233. const seconds = String(date.getSeconds()).padStart(2, '0');
  2234. const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
  2235. const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
  2236. return formattedDateTime;
  2237. };
  2238. /** @enum LoggerLevels 日志输出等级 */
  2239. var LoggerLevels;
  2240. (function (LoggerLevels) {
  2241. LoggerLevels[LoggerLevels["error"] = 0] = "error";
  2242. LoggerLevels[LoggerLevels["warn"] = 1] = "warn";
  2243. LoggerLevels[LoggerLevels["log"] = 2] = "log";
  2244. LoggerLevels[LoggerLevels["debug"] = 3] = "debug";
  2245. })(LoggerLevels || (LoggerLevels = {}));
  2246. /** @class Logger 通用的日志模块 */
  2247. class Logger {
  2248. constructor(level, category, label) {
  2249. Object.defineProperty(this, "_level", {
  2250. enumerable: true,
  2251. configurable: true,
  2252. writable: true,
  2253. value: void 0
  2254. });
  2255. Object.defineProperty(this, "category", {
  2256. enumerable: true,
  2257. configurable: true,
  2258. writable: true,
  2259. value: void 0
  2260. });
  2261. Object.defineProperty(this, "label", {
  2262. enumerable: true,
  2263. configurable: true,
  2264. writable: true,
  2265. value: void 0
  2266. });
  2267. this._level = level;
  2268. this.category = category;
  2269. this.label = label;
  2270. }
  2271. error(content) {
  2272. this.genericLog(LoggerLevels.error, content);
  2273. }
  2274. warn(content) {
  2275. this.genericLog(LoggerLevels.warn, content);
  2276. }
  2277. log(content) {
  2278. this.genericLog(LoggerLevels.log, content);
  2279. }
  2280. debug(content) {
  2281. this.genericLog(LoggerLevels.debug, content);
  2282. }
  2283. genericLog(levelToLog, content) {
  2284. if (this._level >= levelToLog) {
  2285. this.print(levelToLog, this.category, this.label, content);
  2286. }
  2287. }
  2288. print(levelToLog, category, label, content) {
  2289. if (typeof content === 'string') {
  2290. const prefix = [getDateTime()];
  2291. if (label) {
  2292. prefix.push(label);
  2293. }
  2294. content = prefix.concat(content).join(' | ');
  2295. }
  2296. switch (levelToLog) {
  2297. case LoggerLevels.error:
  2298. console.error(`%c${category}`, 'color: blue;', content);
  2299. break;
  2300. case LoggerLevels.warn:
  2301. console.warn(`%c${category}`, 'color: blue;', content);
  2302. break;
  2303. case LoggerLevels.log:
  2304. console.log(`%c${category}`, 'color: blue;', content);
  2305. break;
  2306. case LoggerLevels.debug:
  2307. console.debug(`%c${category}`, 'color: blue;', content);
  2308. break;
  2309. }
  2310. }
  2311. get level() {
  2312. return this._level;
  2313. }
  2314. set level(newLevel) {
  2315. if (newLevel >= 0 && newLevel <= 3) {
  2316. this._level = newLevel;
  2317. } else if (newLevel > 3) {
  2318. this._level = 3;
  2319. // eslint-disable-next-line no-prototype-builtins
  2320. } else if (LoggerLevels.hasOwnProperty(newLevel)) {
  2321. this._level = newLevel;
  2322. } else {
  2323. this.error("invalid 'level' parameter value: " + JSON.stringify(newLevel));
  2324. }
  2325. }
  2326. }
  2327. /**
  2328. * @enum {string} CTI 初始化场景
  2329. * Manual: 手动外呼
  2330. * Robot: 机器人外呼
  2331. * Monitor: 监听
  2332. * Predictive: 预测式外呼
  2333. */
  2334. var Scene;
  2335. (function (Scene) {
  2336. Scene["Manual"] = "manual";
  2337. Scene["Robot"] = "robot";
  2338. Scene["Monitor"] = "monitor";
  2339. Scene["Predictive"] = "predictive";
  2340. Scene["Wechat"] = "wechat";
  2341. })(Scene || (Scene = {}));
  2342. /**
  2343. * @enum {string} CTI 监听场景
  2344. * All: 不区分场景
  2345. * Manual: 手动外呼
  2346. * Robot: 机器人外呼
  2347. * Predictive: 预测式外呼
  2348. */
  2349. var MonitorScene;
  2350. (function (MonitorScene) {
  2351. MonitorScene["All"] = "all";
  2352. MonitorScene["Manual"] = "manual";
  2353. MonitorScene["Robot"] = "robot";
  2354. MonitorScene["Predictive"] = "predictive";
  2355. })(MonitorScene || (MonitorScene = {}));
  2356. /**
  2357. * @interface {} 初始化 SD_CTI 需要的参数
  2358. * loggerLevel: 日志等级
  2359. * password: 临时的鉴权字符串,由业务方写死传进来
  2360. * scene: CTI 初始化场景
  2361. * monitorScene: 监听场景
  2362. * env: 环境变量
  2363. */
  2364. /**
  2365. * @enum {string} Socket 状态
  2366. * Initial: 初始状态
  2367. * Connecting: Socket 开始建立连接
  2368. * Connected: Socket 建立连接成功
  2369. * Ready: 向 IM 发送第一个上行 login 消息收到成功回调
  2370. * Terminated: Socket 连接断开、各种 Socket 错误流转到本状态
  2371. */
  2372. var SocketStatus;
  2373. (function (SocketStatus) {
  2374. SocketStatus["Initial"] = "Initial";
  2375. SocketStatus["Connecting"] = "Connecting";
  2376. SocketStatus["Connected"] = "Connected";
  2377. SocketStatus["Ready"] = "Ready";
  2378. SocketStatus["ReTry"] = "ReTry";
  2379. SocketStatus["Terminated"] = "Terminated";
  2380. })(SocketStatus || (SocketStatus = {}));
  2381. /**
  2382. * @enum {string} SIP 状态
  2383. * Initial: 初始状态
  2384. * Started: SIP 的 User Agent 创建成功
  2385. * Connecting: SIP 底层 Socket 传输 TransportState.Connecting
  2386. * Connected: SIP 底层 Socket 传输 TransportState.Connected
  2387. * Ready: SIP Registerer 监听注册状态 RegistererState.Registered
  2388. * Terminated: SIP Socket 断开、注册失败等各种错误、主动断开连接流转到本状态
  2389. */
  2390. var SIPStatus;
  2391. (function (SIPStatus) {
  2392. SIPStatus["Initial"] = "Initial";
  2393. SIPStatus["Started"] = "Started";
  2394. SIPStatus["Connecting"] = "Connecting";
  2395. SIPStatus["Connected"] = "Connected";
  2396. SIPStatus["Ready"] = "Ready";
  2397. SIPStatus["ReTry"] = "ReTry";
  2398. SIPStatus["Terminated"] = "Terminated";
  2399. })(SIPStatus || (SIPStatus = {}));
  2400. /**
  2401. * @enum {string} CTI 状态
  2402. * Initial: 初始状态
  2403. * Ready: SocketStatus Ready && SIPStatus Ready
  2404. * Terminated: SocketStatus Terminated || SIPStatus Terminated || 正常调用 unInit 方法卸载
  2405. */
  2406. var CTIStatus;
  2407. (function (CTIStatus) {
  2408. CTIStatus["Initial"] = "Initial";
  2409. CTIStatus["Ready"] = "Ready";
  2410. CTIStatus["ReTry"] = "ReTry";
  2411. CTIStatus["Terminated"] = "Terminated";
  2412. })(CTIStatus || (CTIStatus = {}));
  2413. /**
  2414. * @enum {string} 通话状态
  2415. * 为了防止人工外呼方法被二次调用引发预期以外的问题,增加此状态的流转
  2416. * Started: 外呼已开始,此状态下不允许再次发起外呼
  2417. * Stopped: 外呼已结束,此状态下可以再次发起外呼
  2418. */
  2419. var CallStatus;
  2420. (function (CallStatus) {
  2421. CallStatus["Started"] = "Started";
  2422. CallStatus["Stopped"] = "Stopped";
  2423. })(CallStatus || (CallStatus = {}));
  2424. /**
  2425. * @enum {string} Session 状态
  2426. * CTI 目前只有 Invitation(接受会话)的场景,Inviter (主动发起会话)暂时没有
  2427. */
  2428. var SessionStatus;
  2429. (function (SessionStatus) {
  2430. /**
  2431. * If `Inviter`, INVITE not sent yet.
  2432. * If `Invitation`, SDK 收到 INVITE 通话请求,但尚未处理.
  2433. */
  2434. SessionStatus["Initial"] = "Initial";
  2435. /**
  2436. * If `Inviter`, sent INVITE and waiting for a final response.
  2437. * If `Invitation`, received INVITE and attempting to send 200 final response (but has not sent it yet).
  2438. */
  2439. SessionStatus["Establishing"] = "Establishing";
  2440. /**
  2441. * If `Inviter`, sent INVITE and received 200 final response and sent ACK.
  2442. * If `Invitation`, SDK 完成接受 INVITE 并发送 200 OK 确认接起,同时接通本地语音流.
  2443. */
  2444. SessionStatus["Established"] = "Established";
  2445. /**
  2446. * If `Inviter`, sent INVITE, sent CANCEL and now waiting for 487 final response to ACK (or 200 to ACK & BYE).
  2447. * If `Invitation`, received INVITE, sent 200 final response and now waiting on ACK and upon receipt will attempt BYE
  2448. * (as the protocol specification requires, before sending a BYE we must receive the ACK - so we are waiting).
  2449. */
  2450. SessionStatus["Terminating"] = "Terminating";
  2451. /**
  2452. * If `Inviter`, sent INVITE and received non-200 final response (or sent/received BYE after receiving 200).
  2453. * If `Invitation`, SDK 收到 BYE 信令,发送 200 OK 挂断确认,会话结束.
  2454. */
  2455. SessionStatus["Terminated"] = "Terminated";
  2456. })(SessionStatus || (SessionStatus = {}));
  2457. /**
  2458. * @enum {string} CTI 所有错误的分类
  2459. * SdkTerminated: SDK 不可用错误,需要重新初始化
  2460. * SdkError: SDK 状态可用,其他普通错误
  2461. * ServerTerminated: 服务端不可用错误,需要重新初始化,透传服务端 code,msg
  2462. * ServerError: 服务端可用,普通错误,透传服务端 code,msg
  2463. */
  2464. var CTIErrorType;
  2465. (function (CTIErrorType) {
  2466. CTIErrorType["SdkTerminated"] = "SdkTerminated";
  2467. CTIErrorType["SdkError"] = "SdkError";
  2468. CTIErrorType["ServerTerminated"] = "ServerTerminated";
  2469. CTIErrorType["ServerError"] = "ServerError";
  2470. })(CTIErrorType || (CTIErrorType = {}));
  2471. /**
  2472. * @enum {string} SdkTerminated 类型错误的 code 枚举
  2473. * CTITerminated: SDK 状态不可用,CTIStatus 的状态为 Terminated
  2474. * GetUserMedia: 获取坐席媒体权限失败
  2475. * GetInitConfig: 调接口获取 CTI 初始化配置失败
  2476. * SocketOnError: 监听 socket.io 的 error 事件
  2477. * SocketOnConnectError: 监听 socket.io 的 connect_error 事件
  2478. * SocketOnDisconnect: 监听 socket.io 的 disconnect 事件
  2479. * SocketRepeatLogin: 多页面重复登录,IM 互踢事件
  2480. * SIPInitUserAgent: SIP UserAgent 初始化时启动失败
  2481. * SIPInitRegister: SIP Register 初始化时注册失败
  2482. * SIPUserAgentStateStopped: 监听 SIP UserAgent stateChange 事件状态变更为 Stopped
  2483. * SIPTransportStateDisconnected: 监听 SIP Transport StateChange 事件状态变更为 Disconnect
  2484. * SIPRegistererStateTerminated: 监听 SIP Registerer StateChange 事件状态变更为 Terminated
  2485. * SIPOnDisconnect: 监听 SIP OnDisconnect 事件收到异常退出 error
  2486. * SIPInitTransport: SIP Transport 初始化时连接失败
  2487. * SipHeartBeatErr: SIP发送心跳OPTIONS事件时收到异常结果
  2488. * SIPUnRegistered: 注册SIP时失败
  2489. * SocketOnReconnectFailed: Socket重连超过阈值且依然重连失败
  2490. */
  2491. var HskTerminatedCode;
  2492. (function (HskTerminatedCode) {
  2493. HskTerminatedCode["CTITerminated"] = "100001";
  2494. HskTerminatedCode["GetUserMedia"] = "100002";
  2495. HskTerminatedCode["GetInitConfig"] = "100003";
  2496. HskTerminatedCode["SocketOnError"] = "110001";
  2497. HskTerminatedCode["SocketOnConnectError"] = "110002";
  2498. HskTerminatedCode["SocketOnDisconnect"] = "110003";
  2499. HskTerminatedCode["SocketRepeatLogin"] = "110004";
  2500. HskTerminatedCode["SocketOnReconnectFailed"] = "110007";
  2501. HskTerminatedCode["SIPInitUserAgent"] = "120001";
  2502. HskTerminatedCode["SIPInitRegister"] = "120002";
  2503. HskTerminatedCode["SIPUserAgentStateStopped"] = "120003";
  2504. HskTerminatedCode["SIPTransportStateDisconnected"] = "120004";
  2505. HskTerminatedCode["SIPRegistererStateTerminated"] = "120005";
  2506. HskTerminatedCode["SIPOnDisconnect"] = "120006";
  2507. HskTerminatedCode["SIPInitTransport"] = "120007";
  2508. HskTerminatedCode["SipHeartBeatErr"] = "120008";
  2509. HskTerminatedCode["SIPUnRegistered"] = "120009";
  2510. })(HskTerminatedCode || (HskTerminatedCode = {}));
  2511. /**
  2512. * @enum {string} SdkError 类型错误的 code 枚举
  2513. * Answer: SIP accept 接起失败
  2514. * Bye: SIP bye 挂断失败
  2515. * InvitationCancel: SIP Invitation 会话请求被取消
  2516. * AssignStream: 播放语音流失败
  2517. * FetchError: 修饰器handleApiRes当进入到catch时上报此code
  2518. */
  2519. var SdkErrorCode;
  2520. (function (SdkErrorCode) {
  2521. SdkErrorCode["Answer"] = "200001";
  2522. SdkErrorCode["Bye"] = "200002";
  2523. SdkErrorCode["InvitationCancel"] = "200003";
  2524. SdkErrorCode["AssignStream"] = "200004";
  2525. SdkErrorCode["FetchError"] = "200005";
  2526. })(SdkErrorCode || (SdkErrorCode = {}));
  2527. /**
  2528. * @enum {string} CTI 事件推送
  2529. * OnCtiError: CTI 错误事件,含前后端所有错误,SDK 推送
  2530. * OnSessionStatusChange: 坐席侧 SIP 会话状态变更事件,SDK 推送
  2531. * OnInitalSuccess: CTI 初始化成功事件,SDK 推送
  2532. * OnAgentWorkReport: 坐席&用户状态变更事件,Server 推送
  2533. * OnRingStart: 手动外呼用户未接听时,开始播放回铃音,Server 推送
  2534. * OnRingEnd: 手动外呼用户未接听时,播放回铃音结束,Server 推送
  2535. * OnAgentReport: 坐席状态变更事件,Server 推送
  2536. * OnCallReportInfo: 通话时长及通话次数等信息,Server 推送
  2537. * OnDetectedTone: 服务端收到音频信号后推送
  2538. */
  2539. var CTIEvent;
  2540. (function (CTIEvent) {
  2541. CTIEvent["OnCtiError"] = "OnCtiError";
  2542. CTIEvent["OnSessionStatusChange"] = "OnSessionStatusChange";
  2543. CTIEvent["OnInitalSuccess"] = "OnInitalSuccess";
  2544. CTIEvent["OnAgentWorkReport"] = "OnAgentWorkReport";
  2545. CTIEvent["OnRingStart"] = "OnRingStart";
  2546. CTIEvent["OnRingEnd"] = "OnRingEnd";
  2547. CTIEvent["OnDetectedTone"] = "OnDetectedTone";
  2548. CTIEvent["OnAgentReport"] = "OnAgentReport";
  2549. CTIEvent["OnCallReportInfo"] = "OnCallReportInfo";
  2550. // TODO: 后 7 个事件服务端未来不再推送时删掉
  2551. CTIEvent["OnCallRing"] = "OnCallRing";
  2552. CTIEvent["OnCallEnd"] = "OnCallEnd";
  2553. CTIEvent["OnCallAnswer"] = "OnCallAnswer";
  2554. CTIEvent["OnAgentGroupQuery"] = "OnAgentGroupQuery";
  2555. CTIEvent["OnMethodResponseEvent"] = "OnMethodResponseEvent";
  2556. CTIEvent["OnEventPrompt"] = "OnEventPrompt";
  2557. CTIEvent["OnPrompt"] = "OnPrompt";
  2558. })(CTIEvent || (CTIEvent = {}));
  2559. var BaseOption;
  2560. (function (BaseOption) {
  2561. BaseOption["TrackParams"] = "trackParams";
  2562. BaseOption["ENV"] = "env";
  2563. BaseOption["LoggerLevel"] = "loggerLevel";
  2564. })(BaseOption || (BaseOption = {}));
  2565. const items = [[BaseOption.TrackParams, {}], [BaseOption.ENV, 'test'], [BaseOption.LoggerLevel, LoggerLevels.debug]];
  2566. const baseOption = new Map();
  2567. const resetBaseOption = () => {
  2568. items.forEach((i, v) => baseOption.set(i, v));
  2569. };
  2570. resetBaseOption();
  2571. const setBaseOption = (key, value, isInit = false) => {
  2572. if (!isInit && typeof value === 'object') {
  2573. baseOption.set(key, Object.assign(Object.assign({}, baseOption.get(key)), value));
  2574. } else {
  2575. baseOption.set(key, value);
  2576. }
  2577. };
  2578. const getBaseOption = key => {
  2579. return baseOption.get(key);
  2580. };
  2581. const generateUUID = () => {
  2582. return 'xxxxxxxxxxxx4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  2583. const r = Math.random() * 16 | 0;
  2584. const v = c === 'x' ? r : r & 0x3 | 0x8;
  2585. return v.toString(16);
  2586. }).replace(/-/g, '');
  2587. };
  2588. const getClientId = () => {
  2589. if (!window.HS_CTI_CLIENT_ID) {
  2590. window.HS_CTI_CLIENT_ID = generateUUID();
  2591. }
  2592. return window.HS_CTI_CLIENT_ID;
  2593. };
  2594. /**
  2595. * @enum {string} 本地提示音枚举
  2596. * RingAudio: 来电振铃提示音
  2597. * WaitAudio: 主动外呼等待音
  2598. * ByeAudio: 挂断提示音
  2599. */
  2600. var AudioName;
  2601. (function (AudioName) {
  2602. AudioName["RingAudio"] = "_ringAudio";
  2603. AudioName["WaitAudio"] = "_waitAudio";
  2604. AudioName["ByeAudio"] = "_byeAudio";
  2605. })(AudioName || (AudioName = {}));
  2606. /**
  2607. * @enum {string} 埋点上报的分类
  2608. * FeSocket: Socket 类暴露的事件
  2609. * FeSIP: SIP 相关事件
  2610. * FeMethod: 业务调用 SDK 暴露的方法
  2611. * FeAPI: SDK 调用后端的接口返回值
  2612. * FeAPIError: SDK 调用后端的接口错误返回值
  2613. * FeIMDown: 后端通过 IM 返回的常规事件
  2614. * FeIMCmd: IM 返回的断开指令
  2615. * FeMedia: SDK 媒体事件
  2616. * FeStatu: SDK 是否状态流转事件
  2617. * FeEmit: SDK 抛出的事件
  2618. */
  2619. var TrackSource;
  2620. (function (TrackSource) {
  2621. TrackSource["FeSocket"] = "fe-socket";
  2622. TrackSource["FeSIP"] = "fe-sip";
  2623. TrackSource["FeMethod"] = "fe-method";
  2624. TrackSource["FeAPI"] = "fe-api";
  2625. TrackSource["FeAPIError"] = "fe-api-error";
  2626. TrackSource["FeIMDown"] = "fe-im-down";
  2627. TrackSource["FeIMCmd"] = "fe-im-cmd";
  2628. TrackSource["FeMedia"] = "fe-media";
  2629. TrackSource["FeStatus"] = "fe-status";
  2630. TrackSource["FeEmit"] = "fe-emit";
  2631. })(TrackSource || (TrackSource = {}));
  2632. var SocketEvent;
  2633. (function (SocketEvent) {
  2634. SocketEvent["SetSocketStatus"] = "SetSocketStatus";
  2635. SocketEvent["SocketDownEvent"] = "SocketDownEvent";
  2636. })(SocketEvent || (SocketEvent = {}));
  2637. var ExceptMessage;
  2638. (function (ExceptMessage) {
  2639. ExceptMessage["CommonNetworkErrorMsg"] = "\u5BF9\u4E0D\u8D77\uFF0C\u7F51\u7EDC\u72B6\u51B5\u6682\u65F6\u4E0D\u4F73\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\u3002";
  2640. 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";
  2641. ExceptMessage["ManualCallAnswerErrorMsg"] = "\u5F53\u524D\u7535\u8BDD\u672A\u63A5\u901A\uFF0C\u901A\u8BDD\u5DF2\u7ED3\u675F\uFF0C\u8BF7\u91CD\u8BD5\u3002";
  2642. 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";
  2643. ExceptMessage["SipByeErrorMsg"] = "\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u6302\u65AD\u3002";
  2644. ExceptMessage["CTIRepeatLoginMsg"] = "\u5F53\u524D\u5750\u5E2D\u5DF2\u88AB\u5176\u4ED6\u7EC8\u7AEF\u66FF\u4EE3\u3002";
  2645. })(ExceptMessage || (ExceptMessage = {}));
  2646. const methodExceptMsgMap = {
  2647. checkIn: ExceptMessage.CommonNetworkErrorMsg,
  2648. checkOut: ExceptMessage.CommonNetworkErrorMsg,
  2649. setIdle: ExceptMessage.CustomNetworkErrorMsg,
  2650. setBusy: ExceptMessage.CommonNetworkErrorMsg,
  2651. makeCall: ExceptMessage.CommonNetworkErrorMsg,
  2652. answer: ExceptMessage.CommonNetworkErrorMsg,
  2653. bye: ExceptMessage.CommonNetworkErrorMsg,
  2654. loadAgentGroupData: ExceptMessage.CommonNetworkErrorMsg,
  2655. listen: ExceptMessage.CommonNetworkErrorMsg,
  2656. setActiveService: ExceptMessage.CustomNetworkErrorMsg
  2657. };
  2658. const baseRequireParams = ['agent_id', 'saas_id',
  2659. // 'password',
  2660. 'env', 'scene'];
  2661. const monitorRequireParams = ['monitorScene'];
  2662. const allRequiredParams = [...baseRequireParams, ...monitorRequireParams];
  2663. class HsSocket extends EventEmitter {
  2664. constructor(socketOptions) {
  2665. super();
  2666. Object.defineProperty(this, "logger", {
  2667. enumerable: true,
  2668. configurable: true,
  2669. writable: true,
  2670. value: void 0
  2671. });
  2672. Object.defineProperty(this, "socket", {
  2673. enumerable: true,
  2674. configurable: true,
  2675. writable: true,
  2676. value: void 0
  2677. });
  2678. /** 初始化 socket 需要的参数 */
  2679. Object.defineProperty(this, "socketOptions", {
  2680. enumerable: true,
  2681. configurable: true,
  2682. writable: true,
  2683. value: void 0
  2684. });
  2685. /** 心跳延迟时间 */
  2686. Object.defineProperty(this, "heartBeatDelay", {
  2687. enumerable: true,
  2688. configurable: true,
  2689. writable: true,
  2690. value: void 0
  2691. });
  2692. /** 主动关闭链接时间 */
  2693. Object.defineProperty(this, "closeHeartBeatDelay", {
  2694. enumerable: true,
  2695. configurable: true,
  2696. writable: true,
  2697. value: void 0
  2698. });
  2699. /** 心跳检测定时器 */
  2700. Object.defineProperty(this, "heartBeatTimer", {
  2701. enumerable: true,
  2702. configurable: true,
  2703. writable: true,
  2704. value: void 0
  2705. });
  2706. /** 清空心跳检测定时器 */
  2707. Object.defineProperty(this, "closeHeartBeatTimer", {
  2708. enumerable: true,
  2709. configurable: true,
  2710. writable: true,
  2711. value: void 0
  2712. });
  2713. /** 超时次数 */
  2714. // private timeOutCount: number
  2715. /** 最大超时次数限制 */
  2716. Object.defineProperty(this, "imRetryCount", {
  2717. enumerable: true,
  2718. configurable: true,
  2719. writable: true,
  2720. value: void 0
  2721. });
  2722. /** 本次 socket 会话唯一 id */
  2723. Object.defineProperty(this, "sessionId", {
  2724. enumerable: true,
  2725. configurable: true,
  2726. writable: true,
  2727. value: void 0
  2728. });
  2729. /** 页面关闭时 关闭 socket */
  2730. window.addEventListener('onunload', this.closeSocket);
  2731. this.logger = new Logger(socketOptions.loggerLevel, 'HsSocket');
  2732. this.socket = undefined;
  2733. this.heartBeatTimer = null;
  2734. this.closeHeartBeatTimer = null;
  2735. // this.timeOutCount = 0
  2736. this.sessionId = '';
  2737. this.socketOptions = socketOptions;
  2738. this.heartBeatDelay = socketOptions.imHeartTime * 1000 || 3000;
  2739. this.closeHeartBeatDelay = socketOptions.imHeartTime * 1000 || 3000;
  2740. this.imRetryCount = socketOptions.imRetryCount || 10;
  2741. }
  2742. /** @public initSocket 初始化 Socket 连接 */
  2743. initSocket() {
  2744. /** 如果有未断开的连接先断开 */
  2745. this.closeSocket();
  2746. // 设置状态为连接中
  2747. this.emit(SocketEvent.SetSocketStatus, {
  2748. status: SocketStatus.Connecting
  2749. });
  2750. /** https://socket.io/docs/v2/client-api/#iourl */
  2751. this.socket = io(this.socketOptions.imWsServer, {
  2752. transports: ['websocket'],
  2753. reconnectionAttempts: this.imRetryCount,
  2754. reconnectionDelay: this.heartBeatDelay,
  2755. reconnectionDelayMax: this.heartBeatDelay + 1000,
  2756. timeout: this.heartBeatDelay,
  2757. reconnection: true
  2758. });
  2759. /** https://socket.io/docs/v2/client-api/#event-error */
  2760. this.socket.on('error', error => {
  2761. this.logger.error(`socket_error | ${JSON.stringify(error)}`);
  2762. });
  2763. /** https://socket.io/docs/v2/client-api/#event-connect_error-1 */
  2764. this.socket.on('connect_error', error => {
  2765. const errorData = `socket_connect_error | ${JSON.stringify(error)}`;
  2766. this.logger.warn(errorData);
  2767. });
  2768. this.socket.on('reconnecting', res => {
  2769. this.logger.error(`socket_warn | socket_reconnecting | ${res}`);
  2770. this.emit(SocketEvent.SetSocketStatus, {
  2771. status: SocketStatus.ReTry
  2772. });
  2773. });
  2774. this.socket.on('reconnect', res => {
  2775. this.logger.error(`socket_warn | socket_reconnect | ${res}`);
  2776. this.emit(SocketEvent.SetSocketStatus, {
  2777. status: SocketStatus.Ready
  2778. });
  2779. });
  2780. this.socket.on('reconnect_failed', error => {
  2781. this.logger.error(`socket_warn | socket_reconnect_failed | ${error}`);
  2782. this.emit(SocketEvent.SetSocketStatus, {
  2783. status: SocketStatus.Terminated,
  2784. code: HskTerminatedCode.SocketOnReconnectFailed,
  2785. error: `${error}`
  2786. });
  2787. });
  2788. /** https://socket.io/docs/v2/client-api/#event-connect */
  2789. this.socket.on('connect', () => {
  2790. this.emit(SocketEvent.SetSocketStatus, {
  2791. status: SocketStatus.Connected
  2792. });
  2793. this.socketLogin();
  2794. });
  2795. /** https://socket.io/docs/v2/client-api/#event-disconnect */
  2796. this.socket.on('disconnect', reason => {
  2797. const errorMessage = `socket_disconnect | ${reason}`;
  2798. this.logger.warn(errorMessage);
  2799. });
  2800. /** 服务端下行事件 */
  2801. this.socket.on('common_down_data', e => {
  2802. console.log(e, 3434343434);
  2803. if (e && JSON.parse(e) && JSON.parse(e).data) {
  2804. this.emit(SocketEvent.SocketDownEvent, {
  2805. eventData: JSON.parse(JSON.parse(e).data)
  2806. });
  2807. }
  2808. });
  2809. /** 服务端下行指令 */
  2810. this.socket.on('common_down_cmd', e => {
  2811. const {
  2812. clientSessionId
  2813. } = JSON.parse(e);
  2814. console.log('dsdsdsdsdsds', clientSessionId);
  2815. if (clientSessionId === this.sessionId) {
  2816. this.logger.error(`socket status | ${SocketStatus.Terminated} | 坐席在其他页面重新初始化,本页面被踢出`);
  2817. this.emit(SocketEvent.SetSocketStatus, {
  2818. status: SocketStatus.Terminated,
  2819. code: HskTerminatedCode.SocketRepeatLogin,
  2820. error: '您已在其他页面签入,当前页面连接已断开'
  2821. });
  2822. }
  2823. });
  2824. }
  2825. /** @private socketLogin 客户端上行登录事件 */
  2826. socketLogin() {
  2827. const data = {
  2828. // appCode: this.socketOptions.appCode || '1111',
  2829. // token: this.socketOptions.token || '1111',
  2830. userId: this.socketOptions.agent_id
  2831. };
  2832. this.socket && this.socket.emit('login', data, sessionId => {
  2833. console.log(sessionId, '测试一下');
  2834. this.emit(SocketEvent.SetSocketStatus, {
  2835. status: SocketStatus.Ready
  2836. });
  2837. setBaseOption(BaseOption.TrackParams, {
  2838. socket_session_id: sessionId
  2839. });
  2840. this.sessionId = sessionId;
  2841. this.startHeartbeat();
  2842. });
  2843. }
  2844. /** @public closeSocket 关闭 socket 连接 */
  2845. closeSocket() {
  2846. if (this.socket) {
  2847. this.socket.io.opts.reconnection = false;
  2848. this.socket.close();
  2849. // 清除之前的监听事件
  2850. this.socket.removeAllListeners();
  2851. }
  2852. this.socket = undefined;
  2853. this.sessionId = '';
  2854. if (this.heartBeatTimer) {
  2855. window.clearTimeout(this.heartBeatTimer);
  2856. this.heartBeatTimer = null;
  2857. }
  2858. if (this.closeHeartBeatTimer) {
  2859. window.clearTimeout(this.closeHeartBeatTimer);
  2860. this.closeHeartBeatTimer = null;
  2861. }
  2862. }
  2863. /** @private startHeartbeat 开启心跳检测 */
  2864. startHeartbeat() {
  2865. if (this.heartBeatTimer) {
  2866. window.clearTimeout(this.heartBeatTimer);
  2867. this.heartBeatTimer = null;
  2868. }
  2869. if (this.closeHeartBeatTimer) {
  2870. window.clearTimeout(this.closeHeartBeatTimer);
  2871. this.closeHeartBeatTimer = null;
  2872. }
  2873. this.socket && this.heartbeatEvent();
  2874. }
  2875. /** @private heartbeatEvent websocket心跳检测 */
  2876. heartbeatEvent() {
  2877. this.heartBeatTimer = setTimeout(() => {
  2878. this.socket && this.sendHeartbeat();
  2879. /** 如果心跳检测一直没回应,则进行重连 */
  2880. this.closeHeartBeatTimer = setTimeout(() => {
  2881. this.logger.warn('socket_heart_beat | 心跳超时,即将重新连接');
  2882. this.initSocket();
  2883. }, this.closeHeartBeatDelay);
  2884. }, this.heartBeatDelay);
  2885. }
  2886. /** @private sendHeartbeat 客户端上行心跳事件 */
  2887. sendHeartbeat() {
  2888. this.socket && this.socket.emit('heartbeat', JSON.stringify(Object.assign({}, this.socketOptions)), () => {
  2889. this.startHeartbeat();
  2890. });
  2891. }
  2892. }
  2893. const apiLogger = new Logger(window.ctiLoggerLevel || getBaseOption(BaseOption.LoggerLevel), 'HsApi');
  2894. const random16Hex = () => (0x10000 | Math.random() * 0x10000).toString(16).substr(1);
  2895. const random64Hex = () => random16Hex() + random16Hex() + random16Hex() + random16Hex();
  2896. function JPOST({
  2897. baseUrl,
  2898. url,
  2899. data
  2900. }) {
  2901. return __awaiter(this, void 0, void 0, function* () {
  2902. const id = random64Hex();
  2903. const response = yield fetch(baseUrl + url, {
  2904. method: 'POST',
  2905. headers: {
  2906. 'Content-Type': 'application/json',
  2907. 'X-B3-TraceId': id,
  2908. 'X-B3-SpanId': id
  2909. },
  2910. body: JSON.stringify(data)
  2911. });
  2912. if (!response.ok) {
  2913. apiLogger.error(`api response | ${url} | Request failed with status ${response.status}`);
  2914. }
  2915. return response.json();
  2916. });
  2917. }
  2918. const hsTrackJPOST = ({
  2919. baseUrl,
  2920. url = '',
  2921. data
  2922. }) => {
  2923. // eslint-disable-next-line no-async-promise-executor
  2924. return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
  2925. try {
  2926. const res = yield JPOST({
  2927. baseUrl,
  2928. url,
  2929. data
  2930. });
  2931. const {
  2932. code,
  2933. msg
  2934. } = res;
  2935. console.log(msg);
  2936. if (code === 0) {
  2937. apiLogger.log(`api response | ${url} | ${JSON.stringify(res)}`);
  2938. } else {
  2939. apiLogger.error(`api response | ${url} | ${JSON.stringify(res)}`);
  2940. }
  2941. resolve(res);
  2942. } catch (e) {
  2943. apiLogger.error(`api response | ${url} | ${JSON.stringify(e)}`);
  2944. reject(e);
  2945. }
  2946. }));
  2947. };
  2948. // 获取初始化配置
  2949. const baseUrl = 'http://192.168.100.159:8090';
  2950. const getInitConf = data => {
  2951. return hsTrackJPOST({
  2952. baseUrl,
  2953. url: '/open/agent/get-init-config',
  2954. data
  2955. });
  2956. };
  2957. // 坐席签入
  2958. const agentCheckIn = data => {
  2959. return hsTrackJPOST({
  2960. baseUrl,
  2961. url: '/open/agent/check-in',
  2962. data
  2963. });
  2964. };
  2965. // 坐席签出
  2966. const agentCheckOut = data => {
  2967. return hsTrackJPOST({
  2968. baseUrl,
  2969. url: '/open/agent/check-out',
  2970. data
  2971. });
  2972. };
  2973. // 坐席置闲
  2974. const agentSetIdle = data => {
  2975. return hsTrackJPOST({
  2976. baseUrl,
  2977. url: '/open/agent/idle',
  2978. data
  2979. });
  2980. };
  2981. // 坐席置忙
  2982. const agentSetBusy = data => {
  2983. return hsTrackJPOST({
  2984. baseUrl,
  2985. url: '/open/agent/busy',
  2986. data
  2987. });
  2988. };
  2989. // 获取坐席状态
  2990. const getAgentStatus = data => {
  2991. return hsTrackJPOST({
  2992. baseUrl,
  2993. url: '/open/agent/agent-state',
  2994. data
  2995. });
  2996. };
  2997. // 外呼
  2998. const manualCall = data => {
  2999. return hsTrackJPOST({
  3000. baseUrl,
  3001. url: '/open/agent/manual-call',
  3002. data
  3003. });
  3004. };
  3005. // 挂断
  3006. const manualHang = data => {
  3007. return hsTrackJPOST({
  3008. baseUrl,
  3009. url: '/open/agent/manual-hang',
  3010. data
  3011. });
  3012. };
  3013. // 发起监听
  3014. const listen = data => {
  3015. return hsTrackJPOST({
  3016. baseUrl,
  3017. url: '/open/agent/listen',
  3018. data
  3019. });
  3020. };
  3021. // 获取监控组成员信息
  3022. const loadAgentGroupData = data => {
  3023. return hsTrackJPOST({
  3024. baseUrl,
  3025. url: '/open/monitor/load-agent-group-data',
  3026. data
  3027. });
  3028. };
  3029. // 机器人外呼-签入人工组
  3030. const setActiveServiceTask = data => {
  3031. return hsTrackJPOST({
  3032. baseUrl,
  3033. url: '/open/human-service/member-active',
  3034. data
  3035. });
  3036. };
  3037. // 获取 cti 流程 ID
  3038. const getCtiFlowId = data => {
  3039. return hsTrackJPOST({
  3040. baseUrl,
  3041. url: '/open/num/generate',
  3042. data
  3043. });
  3044. };
  3045. /**
  3046. * @function getServerErrorType 根据服务端返回的 code 生成错误类型
  3047. * @param {number} code
  3048. * @returns {CTIErrorType}
  3049. */
  3050. function getServerErrorType(code) {
  3051. if (code >= 300001 && code <= 399999) return CTIErrorType.ServerTerminated;else return CTIErrorType.ServerError;
  3052. }
  3053. /** @function getUserMedia 获取媒体权限 */
  3054. function getUserMedia() {
  3055. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3056. return function (_target, _methodName, descriptor) {
  3057. const originalMethod = descriptor.value;
  3058. descriptor.value = function (...args) {
  3059. if (navigator && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  3060. navigator.mediaDevices.getUserMedia({
  3061. audio: true,
  3062. video: false
  3063. }).then(() => {
  3064. this.logger.debug('media | getUserMedia | 获取浏览器媒体权限成功');
  3065. return originalMethod.apply(this, args);
  3066. }).catch(error => {
  3067. this.logger.error(`media | getUserMedia | ${error}`);
  3068. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3069. type: CTIErrorType.SdkTerminated,
  3070. code: HskTerminatedCode.GetUserMedia,
  3071. msg: error.name === 'NotAllowedError' ? '用户拒绝了获取麦克风权限!' : '获取麦克风权限失败',
  3072. method: 'getUserMedia'
  3073. }, {
  3074. source: TrackSource.FeMedia,
  3075. event_name: 'get_user_media_error',
  3076. error: error.name
  3077. });
  3078. });
  3079. } else {
  3080. this.logger.error('media | getUserMedia | 浏览器版本过低,不支持获取媒体权限');
  3081. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3082. type: CTIErrorType.SdkTerminated,
  3083. code: HskTerminatedCode.GetUserMedia,
  3084. msg: '浏览器版本过低,不支持获取媒体权限',
  3085. method: 'getUserMedia'
  3086. }, {
  3087. source: TrackSource.FeMedia,
  3088. event_name: 'get_user_media_not_support'
  3089. });
  3090. }
  3091. };
  3092. return descriptor;
  3093. };
  3094. }
  3095. /**
  3096. * @function checkCTIStatus 校验 cti 状态
  3097. * @param {string} msg
  3098. */
  3099. function checkCTIStatus(msg) {
  3100. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3101. return function (_target, methodName, descriptor) {
  3102. const originalMethod = descriptor.value;
  3103. descriptor.value = function (...args) {
  3104. /** 调用 SDK method */
  3105. this.logger.log(`sdk method | ${methodName} | ${msg}`);
  3106. /** 校验 CTIStatus, 如果不是 Ready 状态,直接报错 */
  3107. if (this.getCTIStatus !== CTIStatus.Ready) {
  3108. const errorData = {
  3109. type: CTIErrorType.SdkTerminated,
  3110. code: HskTerminatedCode.CTITerminated,
  3111. msg: methodExceptMsgMap[methodName],
  3112. method: methodName,
  3113. terminated_source: this._terminatedStatusList
  3114. };
  3115. this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData);
  3116. return Promise.reject(errorData);
  3117. }
  3118. return originalMethod.apply(this, args);
  3119. };
  3120. return descriptor;
  3121. };
  3122. }
  3123. /** @function handleApiRes 统一处理服务端接口返回值 */
  3124. function handleApiRes() {
  3125. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3126. return function (_target, methodName, descriptor) {
  3127. const originalMethod = descriptor.value;
  3128. descriptor.value = function (...args) {
  3129. return __awaiter(this, void 0, void 0, function* () {
  3130. try {
  3131. const res = yield originalMethod.apply(this, args);
  3132. const {
  3133. code,
  3134. msg
  3135. } = res;
  3136. if (code !== 0) {
  3137. const serverErrorType = getServerErrorType(code);
  3138. const errorData = {
  3139. type: serverErrorType,
  3140. code,
  3141. msg: msg,
  3142. method: methodName
  3143. };
  3144. this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData);
  3145. if (serverErrorType === CTIErrorType.ServerTerminated) {
  3146. this.setCTIStatus(CTIStatus.Terminated);
  3147. }
  3148. return Promise.reject(errorData);
  3149. } else {
  3150. return Promise.resolve(res);
  3151. }
  3152. } catch (error) {
  3153. const errorData = {
  3154. type: 'fetch_error',
  3155. code: SdkErrorCode.FetchError,
  3156. msg: JSON.stringify(error),
  3157. method: methodName
  3158. };
  3159. this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData);
  3160. return Promise.reject(errorData);
  3161. }
  3162. });
  3163. };
  3164. return descriptor;
  3165. };
  3166. }
  3167. function validateParams() {
  3168. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3169. return function (_target, _methodName, descriptor) {
  3170. const originalMethod = descriptor.value;
  3171. descriptor.value = function (...args) {
  3172. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3173. const currentParams = args[0];
  3174. let requireList = [];
  3175. if (currentParams.scene === Scene.Monitor) {
  3176. requireList = allRequiredParams;
  3177. } else {
  3178. requireList = baseRequireParams;
  3179. }
  3180. requireList.forEach(param => {
  3181. if (!currentParams[param]) {
  3182. throw `参数[${param}]为必填参数`;
  3183. }
  3184. });
  3185. return originalMethod.apply(this, args);
  3186. };
  3187. return descriptor;
  3188. };
  3189. }
  3190. function generateUniqueId() {
  3191. return 'id-' + Date.now() + '-' + Math.floor(Math.random() * 10000);
  3192. }
  3193. /**
  3194. * 本地提示音
  3195. * _ringAudio 机器人外呼/监听,等待接起提示音
  3196. * _waitAudio 主动外呼,点击拨打时的等待音
  3197. * _byeAudio 结束通话提示音
  3198. */
  3199. const audioList = {
  3200. _ringAudio: 'http://static.fuxicarbon.com/hs-cti/ring.wav',
  3201. _waitAudio: 'http://static.fuxicarbon.com/hs-cti/manual.wav',
  3202. _byeAudio: 'http://static.fuxicarbon.com/hs-cti/bye.wav'
  3203. };
  3204. /** @class HsCTI 红杉外呼类 */
  3205. class HsCTI extends EventEmitter {
  3206. constructor(hsCTIInitOptions) {
  3207. const {
  3208. saas_id,
  3209. agent_id,
  3210. scene,
  3211. env,
  3212. loggerLevel
  3213. } = hsCTIInitOptions;
  3214. super();
  3215. Object.defineProperty(this, "logger", {
  3216. enumerable: true,
  3217. configurable: true,
  3218. writable: true,
  3219. value: void 0
  3220. });
  3221. Object.defineProperty(this, "loggerLevel", {
  3222. enumerable: true,
  3223. configurable: true,
  3224. writable: true,
  3225. value: void 0
  3226. });
  3227. Object.defineProperty(this, "scene", {
  3228. enumerable: true,
  3229. configurable: true,
  3230. writable: true,
  3231. value: void 0
  3232. });
  3233. Object.defineProperty(this, "agent_id", {
  3234. enumerable: true,
  3235. configurable: true,
  3236. writable: true,
  3237. value: void 0
  3238. });
  3239. Object.defineProperty(this, "saas_id", {
  3240. enumerable: true,
  3241. configurable: true,
  3242. writable: true,
  3243. value: void 0
  3244. });
  3245. /** 接口返回的初始化配置 */
  3246. Object.defineProperty(this, "_initOptions", {
  3247. enumerable: true,
  3248. configurable: true,
  3249. writable: true,
  3250. value: void 0
  3251. });
  3252. /** IM socket的实例 */
  3253. Object.defineProperty(this, "_socket", {
  3254. enumerable: true,
  3255. configurable: true,
  3256. writable: true,
  3257. value: void 0
  3258. });
  3259. /** sip.js UA实例 */
  3260. Object.defineProperty(this, "_sipUserAgent", {
  3261. enumerable: true,
  3262. configurable: true,
  3263. writable: true,
  3264. value: void 0
  3265. });
  3266. Object.defineProperty(this, "_callId", {
  3267. enumerable: true,
  3268. configurable: true,
  3269. writable: true,
  3270. value: void 0
  3271. });
  3272. Object.defineProperty(this, "_ctiFlowIdList", {
  3273. enumerable: true,
  3274. configurable: true,
  3275. writable: true,
  3276. value: void 0
  3277. });
  3278. /** 基本参数 */
  3279. Object.defineProperty(this, "_baseParams", {
  3280. enumerable: true,
  3281. configurable: true,
  3282. writable: true,
  3283. value: void 0
  3284. });
  3285. /** 等待提示音播放器 */
  3286. Object.defineProperty(this, "_waitAudio", {
  3287. enumerable: true,
  3288. configurable: true,
  3289. writable: true,
  3290. value: void 0
  3291. });
  3292. /** 振铃提示音播放器 */
  3293. Object.defineProperty(this, "_ringAudio", {
  3294. enumerable: true,
  3295. configurable: true,
  3296. writable: true,
  3297. value: void 0
  3298. });
  3299. /** 结束通话提示音 */
  3300. Object.defineProperty(this, "_byeAudio", {
  3301. enumerable: true,
  3302. configurable: true,
  3303. writable: true,
  3304. value: void 0
  3305. });
  3306. /** 远端音频流播放器 */
  3307. Object.defineProperty(this, "_remoteAudio", {
  3308. enumerable: true,
  3309. configurable: true,
  3310. writable: true,
  3311. value: void 0
  3312. });
  3313. /** CTI状态 */
  3314. Object.defineProperty(this, "_ctiStatus", {
  3315. enumerable: true,
  3316. configurable: true,
  3317. writable: true,
  3318. value: void 0
  3319. });
  3320. Object.defineProperty(this, "_ctiStatusList", {
  3321. enumerable: true,
  3322. configurable: true,
  3323. writable: true,
  3324. value: void 0
  3325. });
  3326. /** sip状态 */
  3327. Object.defineProperty(this, "_sipStatus", {
  3328. enumerable: true,
  3329. configurable: true,
  3330. writable: true,
  3331. value: void 0
  3332. });
  3333. Object.defineProperty(this, "_sipStatusList", {
  3334. enumerable: true,
  3335. configurable: true,
  3336. writable: true,
  3337. value: void 0
  3338. });
  3339. /** socket状态 */
  3340. Object.defineProperty(this, "_socketStatus", {
  3341. enumerable: true,
  3342. configurable: true,
  3343. writable: true,
  3344. value: void 0
  3345. });
  3346. Object.defineProperty(this, "_socketStatusList", {
  3347. enumerable: true,
  3348. configurable: true,
  3349. writable: true,
  3350. value: void 0
  3351. });
  3352. Object.defineProperty(this, "_callStatus", {
  3353. enumerable: true,
  3354. configurable: true,
  3355. writable: true,
  3356. value: void 0
  3357. });
  3358. Object.defineProperty(this, "_terminatedStatusList", {
  3359. enumerable: true,
  3360. configurable: true,
  3361. writable: true,
  3362. value: void 0
  3363. });
  3364. this.loggerLevel = window.ctiLoggerLevel || loggerLevel || LoggerLevels.log;
  3365. this.logger = new Logger(this.loggerLevel, 'HsCTI');
  3366. this._waitAudio = new Audio();
  3367. this._ringAudio = new Audio();
  3368. this._byeAudio = new Audio();
  3369. this._remoteAudio = new Audio();
  3370. this.saas_id = saas_id;
  3371. this.agent_id = agent_id;
  3372. this.scene = scene;
  3373. this._callId = '';
  3374. setBaseOption(BaseOption.ENV, env);
  3375. setBaseOption(BaseOption.LoggerLevel, this.loggerLevel);
  3376. this._baseParams = {
  3377. agent_id,
  3378. saas_id,
  3379. scene
  3380. };
  3381. const baseTrackParams = {
  3382. agent_id: agent_id,
  3383. vcc_id: saas_id,
  3384. scene
  3385. };
  3386. if (scene === Scene.Monitor) {
  3387. const {
  3388. monitorScene
  3389. } = hsCTIInitOptions;
  3390. this._baseParams = Object.assign(Object.assign({}, this._baseParams), {
  3391. monitorScene
  3392. });
  3393. setBaseOption(BaseOption.TrackParams, Object.assign(Object.assign({}, baseTrackParams), {
  3394. monitor_scene: monitorScene
  3395. }), true);
  3396. } else {
  3397. setBaseOption(BaseOption.TrackParams, baseTrackParams, true);
  3398. }
  3399. this.initInstanceOptions();
  3400. this.setCTIStatus(CTIStatus.Initial);
  3401. // 首次获取实例时,将外呼状态置为未开始
  3402. this._callStatus = CallStatus.Stopped;
  3403. this._terminatedStatusList = [];
  3404. }
  3405. get getCTIStatus() {
  3406. return this._ctiStatus;
  3407. }
  3408. get getSocketStatus() {
  3409. return this._socketStatus;
  3410. }
  3411. get getSIPStatus() {
  3412. return this._sipStatus;
  3413. }
  3414. static getInstance(hsCTIInitOptions) {
  3415. if (!HsCTI.instance) {
  3416. HsCTI.instance = new HsCTI(hsCTIInitOptions);
  3417. }
  3418. setBaseOption(BaseOption.TrackParams, {
  3419. clientId: getClientId()
  3420. });
  3421. return HsCTI.instance;
  3422. }
  3423. /** @private 重置实例 */
  3424. initInstanceOptions() {
  3425. this._ctiFlowIdList = [];
  3426. this._ctiStatusList = [];
  3427. this._sipStatusList = [];
  3428. this._socketStatusList = [];
  3429. HsCTI.instance = undefined;
  3430. this._initOptions = undefined;
  3431. resetBaseOption();
  3432. }
  3433. /** @private 优雅关闭 SIP 和 socket */
  3434. clearSocketAndSip() {
  3435. var _a, _b, _c;
  3436. (_a = this._socket) === null || _a === void 0 ? void 0 : _a.closeSocket();
  3437. (_b = this._sipUserAgent) === null || _b === void 0 ? void 0 : _b.unregister();
  3438. (_c = this._sipUserAgent) === null || _c === void 0 ? void 0 : _c.disconnect();
  3439. this._sipUserAgent = undefined;
  3440. }
  3441. /**
  3442. * @private 设置等待音 src
  3443. * @param {AudioName} audioName
  3444. * @param {boolean} loop
  3445. */
  3446. setAudioSrc(audioName, loop) {
  3447. this.logger.debug(`media | 设置等待音 src: ${audioList[audioName]}`);
  3448. this[audioName].src = audioList[audioName];
  3449. this[audioName].currentTime = 0;
  3450. this[audioName].autoplay = false;
  3451. this[audioName].loop = loop;
  3452. }
  3453. playAudio(audioName) {
  3454. this.logger.debug(`media | 播放音频:${audioName}`);
  3455. this[audioName].play();
  3456. }
  3457. stopLocalAudio() {
  3458. switch (this.scene) {
  3459. case Scene.Robot:
  3460. case Scene.Monitor:
  3461. this.stopAudio(AudioName.RingAudio, true);
  3462. break;
  3463. case Scene.Manual:
  3464. this.stopAudio(AudioName.WaitAudio, true);
  3465. break;
  3466. }
  3467. }
  3468. stopAudio(audioName, loop) {
  3469. this.logger.debug(`media | 停止音频播放:${audioName}`);
  3470. this[audioName].src = '';
  3471. setTimeout(() => {
  3472. this.setAudioSrc(audioName, loop);
  3473. }, 1000);
  3474. }
  3475. init() {
  3476. // 不允许重复初始化,必须在销毁sdk后才能重新初始化
  3477. const [lastStatus] = this._ctiStatusList.slice(-1);
  3478. if (lastStatus !== CTIStatus.Initial) return;
  3479. this.setAudioSrc(AudioName.ByeAudio, false);
  3480. switch (this.scene) {
  3481. case Scene.Robot:
  3482. case Scene.Monitor:
  3483. this.setAudioSrc(AudioName.RingAudio, true);
  3484. break;
  3485. case Scene.Manual:
  3486. this.setAudioSrc(AudioName.WaitAudio, true);
  3487. break;
  3488. }
  3489. this.getInitConfig();
  3490. }
  3491. getInitConfig() {
  3492. return __awaiter(this, void 0, void 0, function* () {
  3493. const res = yield getInitConf(this._baseParams);
  3494. const {
  3495. code,
  3496. data,
  3497. msg
  3498. } = res;
  3499. let initOptions = data;
  3500. /**
  3501. * 由于后端接口层面对于查询来说,查不到不代表异常,所以即使查不到也是正向流程
  3502. * 因此如果传入一个不存在的agent_id,后端也会返回code=0,但是data=null
  3503. * 这里需要根据code===0&&initOptions存在进行联合判断,只有两个条件都成立,才表示该坐席获取fs以及im等配置成功
  3504. */
  3505. initOptions = Object.assign(Object.assign({}, initOptions), {
  3506. imHeartTime: 3,
  3507. // IM 重试次数
  3508. imRetryCount: 3,
  3509. // FS 心跳间隔
  3510. fsHeartTime: 60,
  3511. // FS 重试次数,
  3512. fsRetryCount: 3,
  3513. // FS 重试间隔时间
  3514. fsRetryTime: 60,
  3515. // FS 注册过期时间
  3516. fsRegisterExpireTime: 84000,
  3517. // 单次初始化唯一 ID
  3518. ctiSessionId: generateUniqueId(),
  3519. // IM websocket url
  3520. imWsServer: 'ws://192.168.100.159:8091/ws/cs-im',
  3521. // IM websocket path
  3522. imWsPath: 'ws/cs-im'
  3523. });
  3524. if (code === 0 && initOptions) {
  3525. setBaseOption(BaseOption.TrackParams, {
  3526. sip_server: initOptions.sipServer,
  3527. cti_session_id: initOptions.ctiSessionId
  3528. });
  3529. this._initOptions = initOptions;
  3530. this.initSocket(initOptions);
  3531. this.initSip(initOptions);
  3532. } else {
  3533. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3534. type: CTIErrorType.ServerTerminated,
  3535. code: HskTerminatedCode.GetInitConfig,
  3536. // 如果code===0并且initOptions不存在时,代表未查到对应坐席,此时接口返回的msg是空字符串,所以提示文案要特定处理
  3537. msg: code === 0 && !initOptions ? '未查到坐席' : msg,
  3538. method: 'getInitConfig',
  3539. res_code: code
  3540. });
  3541. }
  3542. });
  3543. }
  3544. initSocket(initOptions) {
  3545. this.setSocketStatus({
  3546. status: SocketStatus.Initial
  3547. });
  3548. this._socket = new HsSocket({
  3549. agent_id: this.agent_id,
  3550. saas_id: this.saas_id,
  3551. // appCode: initOptions.appCode,
  3552. imWsServer: initOptions.imWsServer,
  3553. imWsPath: initOptions.imWsPath,
  3554. imHeartTime: initOptions.imHeartTime,
  3555. imRetryCount: initOptions.imRetryCount,
  3556. ctiSessionId: initOptions.ctiSessionId,
  3557. loggerLevel: this.loggerLevel
  3558. });
  3559. this._socket.on(SocketEvent.SocketDownEvent, ({
  3560. eventData
  3561. }) => {
  3562. const {
  3563. eventName,
  3564. ext
  3565. } = eventData;
  3566. this.logger.log(`socket server down | ${eventName} | ${JSON.stringify(ext)}`);
  3567. this.handleSocketDownEvent({
  3568. eventName,
  3569. ext
  3570. });
  3571. });
  3572. this._socket.on(SocketEvent.SetSocketStatus, res => {
  3573. console.log(`set socket statussdsdsdsdsdsdds | ${JSON.stringify(res)}`);
  3574. this.setSocketStatus(res);
  3575. });
  3576. this._socket.initSocket();
  3577. }
  3578. handleSocketDownEvent({
  3579. eventName,
  3580. ext
  3581. }) {
  3582. const ctiFlowId = this._baseParams.ctiFlowId;
  3583. const extCtiFlowId = ext.ctiFlowId;
  3584. if ([Scene.Manual, Scene.Monitor].includes(this.scene) && ctiFlowId && extCtiFlowId && ctiFlowId !== extCtiFlowId) {
  3585. this.logger.error(`cti_flow_id | 不一致! fe: ${ctiFlowId}, server: ${extCtiFlowId}, eventName: ${eventName}`);
  3586. return;
  3587. }
  3588. this.serverEventEmit({
  3589. eventName,
  3590. ext
  3591. });
  3592. }
  3593. /** @private serverEventEmit 统一处理服务端推送的事件 */
  3594. serverEventEmit({
  3595. eventName,
  3596. ext
  3597. }) {
  3598. const NO_EMIT_EVENT_LIST = [CTIEvent.OnRingStart, CTIEvent.OnDetectedTone, CTIEvent.OnRingEnd];
  3599. switch (eventName) {
  3600. case CTIEvent.OnRingStart:
  3601. this.stopLocalAudio();
  3602. break;
  3603. case CTIEvent.OnDetectedTone:
  3604. this.stopLocalAudio();
  3605. break;
  3606. case CTIEvent.OnAgentWorkReport:
  3607. break;
  3608. case CTIEvent.OnCallEnd:
  3609. /** 通话结束:重置可拨打状态,清空本轮通话的CallId */
  3610. this._callStatus = CallStatus.Stopped;
  3611. setBaseOption(BaseOption.TrackParams, {
  3612. call_id: ''
  3613. });
  3614. break;
  3615. case CTIEvent.OnRingEnd:
  3616. /** TODO: 后 4 个事件未来服务端不再推送时删掉 */
  3617. break;
  3618. // do nothing
  3619. }
  3620. if (!NO_EMIT_EVENT_LIST.includes(eventName)) {
  3621. this.eventEmitAndTrack(eventName, ext);
  3622. }
  3623. }
  3624. initSip(initOptions) {
  3625. this.setSipStatus({
  3626. status: SIPStatus.Initial
  3627. });
  3628. const server = initOptions.wss_server;
  3629. const simpleUserPlusOptions = {
  3630. aor: initOptions.sip_server,
  3631. reconnectionAttempts: initOptions.fsRetryCount,
  3632. reconnectionDelay: initOptions.fsRetryTime,
  3633. optionsPingInterval: initOptions.fsHeartTime,
  3634. media: {
  3635. remote: {
  3636. audio: this._remoteAudio
  3637. }
  3638. },
  3639. userAgentOptions: {
  3640. gracefulShutdown: true,
  3641. authorizationPassword: initOptions.phone_pwd,
  3642. sessionDescriptionHandlerFactoryOptions: {
  3643. constraints: {
  3644. audio: {
  3645. echoCancellation: true,
  3646. noiseSuppression: true,
  3647. autoGainControl: true
  3648. },
  3649. video: false
  3650. },
  3651. peerConnectionConfiguration: {
  3652. // iceServers: []
  3653. iceServers: [{
  3654. urls: initOptions.ice_server
  3655. }]
  3656. }
  3657. }
  3658. }
  3659. };
  3660. simpleUserPlusOptions.delegate = this.sipDelegate();
  3661. this._sipUserAgent = new SimpleUserPlus(server, simpleUserPlusOptions);
  3662. this._sipUserAgent.connect().catch(error => {
  3663. const err = `SIP UserAgent 启动失败:${JSON.stringify(error)}`;
  3664. this.logger.error(err);
  3665. this.setSipStatus({
  3666. status: SIPStatus.Terminated,
  3667. code: HskTerminatedCode.SIPInitUserAgent,
  3668. error: err,
  3669. method: 'initSIPJS'
  3670. });
  3671. });
  3672. }
  3673. sipDelegate() {
  3674. return {
  3675. onServerConnect: () => {
  3676. this._sipUserAgent.register();
  3677. },
  3678. onServerDisconnect: () => {
  3679. // 正向关闭UA时会进入此回调
  3680. this.setSipStatus({
  3681. status: SIPStatus.Terminated
  3682. });
  3683. },
  3684. onReconnectStart: () => {
  3685. // 开始重连时将状态流转为ReTry
  3686. this.setSipStatus({
  3687. status: SIPStatus.ReTry
  3688. });
  3689. },
  3690. onReconnectFailed: () => {
  3691. // 重连失败时会进如此回调
  3692. this.setSipStatus({
  3693. status: SIPStatus.Terminated,
  3694. code: HskTerminatedCode.SIPOnDisconnect,
  3695. error: `超过重连次数,终止重连`,
  3696. method: 'onReconnectFailed'
  3697. });
  3698. this.setSipStatus({
  3699. status: SIPStatus.Terminated
  3700. });
  3701. },
  3702. onRegistered: () => {
  3703. this.setSipStatus({
  3704. status: SIPStatus.Ready
  3705. });
  3706. },
  3707. onUserAgentStateChange: state => {
  3708. switch (state) {
  3709. case UserAgentState.Started:
  3710. this.setSipStatus({
  3711. status: SIPStatus.Started
  3712. });
  3713. break;
  3714. case UserAgentState.Stopped:
  3715. this.setSipStatus({
  3716. status: SIPStatus.Terminated,
  3717. code: this._sipStatusList.includes(SIPStatus.Started) ? HskTerminatedCode.SIPUserAgentStateStopped : HskTerminatedCode.SIPInitUserAgent,
  3718. error: 'SIP UserAgentState 状态停止',
  3719. method: 'onUserAgentStateChange'
  3720. });
  3721. break;
  3722. }
  3723. },
  3724. onInvite: invitation => {
  3725. // this.scene = Scene.Robot
  3726. const callId = invitation.request.getHeader('P-LIBRA-CallId') || '';
  3727. console.log(callId, 2888888888);
  3728. // const ctiFlowId =
  3729. // invitation.request.getHeader('P-LIBRA-CtiFlowId') || ''
  3730. // if (ctiFlowId != this._baseParams.ctiFlowId) {
  3731. // this.logger.error(
  3732. // `cti_flow_id 不一致! fe: ${this._baseParams.ctiFlowId} | P-LIBRA-CtiFlowId: ${ctiFlowId}`
  3733. // )
  3734. // return
  3735. // }
  3736. this._callId = callId;
  3737. console.log(callId, 1888888888);
  3738. setBaseOption(BaseOption.TrackParams, {
  3739. call_id: callId
  3740. });
  3741. this.sessionStateChangeAndTrack(invitation.state);
  3742. if ([Scene.Robot, Scene.Monitor].includes(this.scene)) {
  3743. this.playAudio(AudioName.RingAudio);
  3744. console.log('playAudio', 1888888888);
  3745. }
  3746. if ([Scene.Manual].includes(this.scene)) {
  3747. this.answer();
  3748. console.log('answer', 1888888888);
  3749. }
  3750. invitation.delegate = {
  3751. onCancel: cancel => {
  3752. const error = `sip_invitation_on_cancel | ${JSON.stringify(cancel)}`;
  3753. this.logger.error(error);
  3754. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3755. type: CTIErrorType.SdkError,
  3756. code: SdkErrorCode.InvitationCancel,
  3757. msg: '当前通话已结束',
  3758. method: 'sipDelegate'
  3759. }, error);
  3760. }
  3761. };
  3762. invitation.stateChange.addListener(state => {
  3763. this.sessionStateChangeAndTrack(state);
  3764. switch (state) {
  3765. case SessionState.Initial:
  3766. case SessionState.Establishing:
  3767. break;
  3768. case SessionState.Established:
  3769. if ([Scene.Robot, Scene.Monitor].includes(this.scene)) {
  3770. this.stopLocalAudio();
  3771. }
  3772. break;
  3773. case SessionState.Terminating:
  3774. break;
  3775. case SessionState.Terminated:
  3776. this.playAudio(AudioName.ByeAudio);
  3777. setTimeout(() => {
  3778. this.stopAudio(AudioName.ByeAudio, false);
  3779. }, 1000);
  3780. this.stopLocalAudio();
  3781. this._callStatus = CallStatus.Stopped;
  3782. setBaseOption(BaseOption.TrackParams, {
  3783. call_id: ''
  3784. });
  3785. break;
  3786. }
  3787. });
  3788. }
  3789. };
  3790. }
  3791. sessionStateChangeAndTrack(status) {
  3792. this.eventEmitAndTrack(CTIEvent.OnSessionStatusChange, {
  3793. status
  3794. });
  3795. const trackName = `sip_session_state_${upperCamelToLowerSnake(status)}`;
  3796. this.logger.log(trackName);
  3797. }
  3798. /**
  3799. * @private setCTIStatus CTI 状态流转
  3800. * @param {CTIStatus} ctiStatus
  3801. */
  3802. setCTIStatus(ctiStatus) {
  3803. if (ctiStatus === this.getCTIStatus) return;
  3804. this._ctiStatus = ctiStatus;
  3805. this._ctiStatusList.push(ctiStatus);
  3806. const logInfo = `cti status | ${ctiStatus} | ${this._ctiStatusList}`;
  3807. if (ctiStatus === CTIStatus.Terminated) {
  3808. this.setSocketStatus({
  3809. status: SocketStatus.Terminated
  3810. });
  3811. this.setSipStatus({
  3812. status: SIPStatus.Terminated
  3813. });
  3814. this.stopLocalAudio();
  3815. this.logger.warn(logInfo);
  3816. } else {
  3817. this.logger.debug(logInfo);
  3818. }
  3819. if (ctiStatus === CTIStatus.Ready) {
  3820. this.setSocketStatus({
  3821. status: SocketStatus.Ready
  3822. });
  3823. this.setSipStatus({
  3824. status: SIPStatus.Ready
  3825. });
  3826. this._terminatedStatusList = [];
  3827. if (!this._ctiStatusList.includes(CTIStatus.ReTry)) {
  3828. this.checkIn();
  3829. }
  3830. }
  3831. }
  3832. /**
  3833. * @private setSocketStatus Socket 状态流转
  3834. * @param SocketStatusChangeParams Socket 状态流转参数及错误详情等
  3835. */
  3836. setSocketStatus({
  3837. status,
  3838. code,
  3839. error
  3840. }) {
  3841. if (status === this.getSocketStatus) return;
  3842. this._socketStatus = status;
  3843. this._socketStatusList.push(status);
  3844. const logInfo = `socket status | ${status} | ${this._socketStatusList}`;
  3845. status === SocketStatus.Terminated ? this.logger.warn(logInfo) : this.logger.debug(logInfo);
  3846. this.socketOrSipStatusChange(status, this.getSIPStatus);
  3847. if (error) {
  3848. this._terminatedStatusList.push(code);
  3849. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3850. type: CTIErrorType.SdkTerminated,
  3851. code,
  3852. msg: code === HskTerminatedCode.SocketRepeatLogin ? ExceptMessage.CTIRepeatLoginMsg : ExceptMessage.CommonNetworkErrorMsg,
  3853. method: 'setSocketStatus'
  3854. }, {
  3855. error_msg: error,
  3856. socket_status_list: this._socketStatusList,
  3857. sip_status_list: this._sipStatusList,
  3858. cti_status_list: this._ctiStatusList
  3859. });
  3860. this.clearSocketAndSip();
  3861. }
  3862. }
  3863. /**
  3864. * @private setSipStatus SIP 状态流转
  3865. * @param SIPStatusChangeParams SIP 状态流转参数及错误详情等
  3866. */
  3867. setSipStatus({
  3868. status,
  3869. code,
  3870. error,
  3871. method
  3872. }) {
  3873. if (status === this.getSIPStatus) return;
  3874. this._sipStatus = status;
  3875. this._sipStatusList.push(status);
  3876. const logInfo = `sip status | ${status} | ${this._sipStatusList}`;
  3877. status === SIPStatus.Terminated ? this.logger.warn(logInfo) : this.logger.debug(logInfo);
  3878. this.socketOrSipStatusChange(this.getSocketStatus, status);
  3879. if (error) {
  3880. /** 抛出 SIP 类型的错误详情 */
  3881. this._terminatedStatusList.push(code);
  3882. this.eventEmitAndTrack(CTIEvent.OnCtiError, {
  3883. type: CTIErrorType.SdkTerminated,
  3884. code,
  3885. msg: ExceptMessage.CommonNetworkErrorMsg,
  3886. method: method || 'setSipStatus'
  3887. }, {
  3888. error_msg: error,
  3889. socket_status_list: this._socketStatusList,
  3890. sip_status_list: this._sipStatusList,
  3891. cti_status_list: this._ctiStatusList
  3892. });
  3893. this.clearSocketAndSip();
  3894. }
  3895. }
  3896. /**
  3897. * @private socketOrSipStatusChange Socket 或 SIP 状态变化可能引起 CTI 状态变化
  3898. * @param {SocketStatus} socketStatus
  3899. * @param {SIPStatus} sipStatus
  3900. */
  3901. socketOrSipStatusChange(socketStatus, sipStatus) {
  3902. if (socketStatus === SocketStatus.Ready && sipStatus === SIPStatus.Ready) {
  3903. this.setCTIStatus(CTIStatus.Ready);
  3904. }
  3905. // if (socketStatus === SocketStatus.Ready) {
  3906. // this.setCTIStatus(CTIStatus.Ready)
  3907. // }
  3908. if (socketStatus === SocketStatus.ReTry || sipStatus === SIPStatus.ReTry) {
  3909. this.setCTIStatus(CTIStatus.ReTry);
  3910. }
  3911. if (socketStatus === SocketStatus.Terminated || sipStatus === SIPStatus.Terminated) {
  3912. this.setCTIStatus(CTIStatus.Terminated);
  3913. }
  3914. }
  3915. /**
  3916. * @private eventEmitAndTrack SDK 对外抛出事件并统一埋点
  3917. * @param {CTIEvent} eventName 事件名称
  3918. * @param {object} ext 事件参数
  3919. * @param { error } error 错误详情或错误辅助信息
  3920. */
  3921. eventEmitAndTrack(eventName, ext, error) {
  3922. this.logger.debug(`sdk emit | ${eventName} | ${JSON.stringify(ext)}`);
  3923. // 如果出现异常,则将通话状态置为结束
  3924. if (eventName === CTIEvent.OnCtiError) {
  3925. this._callStatus = CallStatus.Stopped;
  3926. }
  3927. try {
  3928. this.emit(eventName, ext);
  3929. console.log(error);
  3930. } catch (error) {
  3931. this.logger.error(`业务监听 ${eventName} 事件,处理回调时报错: ${error}`);
  3932. }
  3933. }
  3934. /** @private checkIn 服务端签入,CTIStatus Ready 时自动调用,坐席状态变更 */
  3935. checkIn() {
  3936. return __awaiter(this, void 0, void 0, function* () {
  3937. const res = yield agentCheckIn(this._baseParams);
  3938. if (res.code === 0) {
  3939. this.eventEmitAndTrack(CTIEvent.OnInitalSuccess, {
  3940. saas_id: this.saas_id,
  3941. agent_id: this.agent_id,
  3942. scene: this.scene,
  3943. phoneNum: this._initOptions.phone_num,
  3944. sipServer: this._initOptions.sip_server
  3945. });
  3946. }
  3947. return res;
  3948. });
  3949. }
  3950. /** @public checkOut 服务端签出, unInit 时自动调用,坐席状态变更 */
  3951. checkOut() {
  3952. return __awaiter(this, void 0, void 0, function* () {
  3953. return yield agentCheckOut(this._baseParams);
  3954. });
  3955. }
  3956. /** @private _getCtiFlowId 获取手动外呼场景需要的 ctiFlowId */
  3957. getCtiFlowId() {
  3958. return __awaiter(this, void 0, void 0, function* () {
  3959. const res = yield getCtiFlowId(this._baseParams);
  3960. const {
  3961. code,
  3962. data
  3963. } = res;
  3964. if (code === 0) {
  3965. this._baseParams.ctiFlowId = data;
  3966. this._ctiFlowIdList.push(data);
  3967. setBaseOption(BaseOption.TrackParams, {
  3968. cti_flow_id: data,
  3969. cti_flow_id_list: JSON.stringify(this._ctiFlowIdList)
  3970. });
  3971. }
  3972. return res;
  3973. });
  3974. }
  3975. /** @public setIdle 服务端置闲,坐席状态变更 */
  3976. setIdle() {
  3977. return __awaiter(this, void 0, void 0, function* () {
  3978. return yield agentSetIdle(this._baseParams);
  3979. });
  3980. }
  3981. /** @public setBusy 服务端置忙,坐席状态变更 */
  3982. setBusy() {
  3983. return __awaiter(this, void 0, void 0, function* () {
  3984. return yield agentSetBusy(this._baseParams);
  3985. });
  3986. }
  3987. makeCall(params) {
  3988. return __awaiter(this, void 0, void 0, function* () {
  3989. // 如果当前通话状态处于外呼开始,则不允许再次调用此方法并上报埋点
  3990. if (this._callStatus === CallStatus.Started) {
  3991. return;
  3992. }
  3993. this._callStatus = CallStatus.Started;
  3994. yield this.getCtiFlowId();
  3995. this.playAudio(AudioName.WaitAudio);
  3996. return yield this.serverCall(params);
  3997. });
  3998. }
  3999. serverCall({
  4000. called,
  4001. caller,
  4002. ext
  4003. }) {
  4004. return __awaiter(this, void 0, void 0, function* () {
  4005. const params = Object.assign(Object.assign(Object.assign({}, this._baseParams), {
  4006. called,
  4007. caller
  4008. }), ext);
  4009. const res = yield manualCall(params);
  4010. const {
  4011. code,
  4012. data
  4013. } = res;
  4014. if (code === 0) {
  4015. if (this._callId === '' && data) {
  4016. this._callId = data;
  4017. setBaseOption(BaseOption.TrackParams, {
  4018. call_id: data
  4019. });
  4020. }
  4021. } else {
  4022. // 外呼接口调用失败将通话状态置为结束不阻碍下次外呼
  4023. this._callStatus = CallStatus.Stopped;
  4024. this.stopLocalAudio();
  4025. }
  4026. return res;
  4027. });
  4028. }
  4029. /** @public answer SDK SIP 接起 */
  4030. answer() {
  4031. return new Promise((resolve, reject) => {
  4032. var _a;
  4033. (_a = this._sipUserAgent) === null || _a === void 0 ? void 0 : _a.answer({
  4034. sessionDescriptionHandlerOptions: {
  4035. constraints: {
  4036. audio: true,
  4037. video: false
  4038. }
  4039. }
  4040. }).then(() => {
  4041. resolve({
  4042. code: 0,
  4043. data: 'answer',
  4044. msg: 'SIP 接起电话成功'
  4045. });
  4046. this.logger.debug('sip_accept_success');
  4047. }).catch(err => {
  4048. const errorData = {
  4049. type: CTIErrorType.SdkError,
  4050. code: SdkErrorCode.Answer,
  4051. msg: this.scene === Scene.Manual ? ExceptMessage.ManualCallAnswerErrorMsg : ExceptMessage.RobotOrWeChatAnswerErrorMsg,
  4052. method: 'answer'
  4053. };
  4054. reject(errorData);
  4055. this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData, `${err}`);
  4056. this.logger.error(`${CTIEvent.OnCtiError} | ${err}`);
  4057. });
  4058. });
  4059. }
  4060. /** @public bye SDK SIP 挂断 */
  4061. bye() {
  4062. return new Promise((resolve, reject) => {
  4063. var _a;
  4064. (_a = this._sipUserAgent) === null || _a === void 0 ? void 0 : _a.hangup().then(() => {
  4065. resolve({
  4066. code: 0,
  4067. data: 'bye',
  4068. msg: 'SIP 挂断电话成功'
  4069. });
  4070. this.logger.debug('sip_bye_success');
  4071. }).catch(err => {
  4072. const errorData = {
  4073. type: CTIErrorType.SdkError,
  4074. code: SdkErrorCode.Bye,
  4075. msg: ExceptMessage.SipByeErrorMsg,
  4076. method: 'bye'
  4077. };
  4078. reject(errorData);
  4079. if (err.message !== 'Session does not exist.') {
  4080. this.eventEmitAndTrack(CTIEvent.OnCtiError, errorData, `${err}`);
  4081. }
  4082. this.logger.error(`${CTIEvent.OnCtiError} | ${err}`);
  4083. });
  4084. });
  4085. }
  4086. /** @public serverBye 挂断且流转坐席状态 */
  4087. serverBye() {
  4088. return __awaiter(this, void 0, void 0, function* () {
  4089. try {
  4090. this.bye();
  4091. // 如果人工外呼场景下,没有flowId则不调用并上报埋点
  4092. if (!this._baseParams.ctiFlowId && this.scene === Scene.Manual) {
  4093. return;
  4094. }
  4095. const res = yield this.turnHang();
  4096. return Promise.resolve(res);
  4097. } catch (error) {
  4098. return Promise.reject(error);
  4099. }
  4100. });
  4101. }
  4102. /** @public turnHang 流转坐席状态-通话结束 */
  4103. turnHang() {
  4104. return __awaiter(this, void 0, void 0, function* () {
  4105. const res = yield manualHang(Object.assign(Object.assign({}, this._baseParams), {
  4106. callId: this._callId
  4107. }));
  4108. this._callId = '';
  4109. return res;
  4110. });
  4111. }
  4112. /** @public getAgentStatus 获取坐席状态 */
  4113. getAgentStatus() {
  4114. return __awaiter(this, void 0, void 0, function* () {
  4115. return yield getAgentStatus(this._baseParams);
  4116. });
  4117. }
  4118. /**
  4119. * @public loadAgentGroupData 监听-根据监听组 ID 获取监听组成员
  4120. * @param {string[]} monitorIds
  4121. */
  4122. loadAgentGroupData(monitorIds) {
  4123. return __awaiter(this, void 0, void 0, function* () {
  4124. return yield loadAgentGroupData(Object.assign(Object.assign({}, this._baseParams), {
  4125. monitorIds
  4126. }));
  4127. });
  4128. }
  4129. /**
  4130. * @public listen 监听-服务端发起监听
  4131. * @param {string} monitoredAgNo
  4132. */
  4133. listen(monitoredAgNo) {
  4134. return __awaiter(this, void 0, void 0, function* () {
  4135. const flowIdRes = yield this.getCtiFlowId();
  4136. if (flowIdRes && flowIdRes.code !== 0) {
  4137. return flowIdRes;
  4138. }
  4139. return yield listen(Object.assign(Object.assign({}, this._baseParams), {
  4140. agent_id: monitoredAgNo,
  4141. leaderAgentId: this.agent_id
  4142. }));
  4143. });
  4144. }
  4145. /**
  4146. * @public setActiveService 机器人外呼-签入人工组
  4147. * @param {string} serviceId
  4148. */
  4149. setActiveService(serviceId) {
  4150. return __awaiter(this, void 0, void 0, function* () {
  4151. return yield setActiveServiceTask(Object.assign(Object.assign({}, this._baseParams), {
  4152. serviceId
  4153. }));
  4154. });
  4155. }
  4156. /** @public unInit 卸载 SDK,checkOut 成功后断开 socket 和 sip 连接,并销毁 SdCTI 实例 */
  4157. unInit() {
  4158. return __awaiter(this, void 0, void 0, function* () {
  4159. yield this.checkOut();
  4160. this.setCTIStatus(CTIStatus.Terminated);
  4161. this.initInstanceOptions();
  4162. this.clearSocketAndSip();
  4163. this._callId = '';
  4164. });
  4165. }
  4166. }
  4167. __decorate([getUserMedia()], HsCTI.prototype, "init", null);
  4168. __decorate([checkCTIStatus('签入'), handleApiRes()], HsCTI.prototype, "checkIn", null);
  4169. __decorate([checkCTIStatus('签出'), handleApiRes()], HsCTI.prototype, "checkOut", null);
  4170. __decorate([handleApiRes()], HsCTI.prototype, "getCtiFlowId", null);
  4171. __decorate([checkCTIStatus('置闲'), handleApiRes()], HsCTI.prototype, "setIdle", null);
  4172. __decorate([checkCTIStatus('置忙'), handleApiRes()], HsCTI.prototype, "setBusy", null);
  4173. __decorate([checkCTIStatus('人工外呼')], HsCTI.prototype, "makeCall", null);
  4174. __decorate([handleApiRes()], HsCTI.prototype, "serverCall", null);
  4175. __decorate([checkCTIStatus('接起')], HsCTI.prototype, "answer", null);
  4176. __decorate([checkCTIStatus('挂断')], HsCTI.prototype, "bye", null);
  4177. __decorate([handleApiRes()], HsCTI.prototype, "turnHang", null);
  4178. __decorate([handleApiRes()], HsCTI.prototype, "getAgentStatus", null);
  4179. __decorate([checkCTIStatus('获取监听组成员'), handleApiRes()], HsCTI.prototype, "loadAgentGroupData", null);
  4180. __decorate([checkCTIStatus('坐席监听'), handleApiRes()], HsCTI.prototype, "listen", null);
  4181. __decorate([checkCTIStatus('签入人工组'), handleApiRes()], HsCTI.prototype, "setActiveService", null);
  4182. __decorate([validateParams()], HsCTI, "getInstance", null);
  4183. /**
  4184. * @function getInstance 获取 HsCTI 的实例
  4185. * @param HsCTIInitOptions 初始化 HsCTI 的配置
  4186. */
  4187. const getInstance = HsCTIInitOptions => HsCTI.getInstance(HsCTIInitOptions);
  4188. export { CTIErrorType, CTIEvent, CTIStatus, CallStatus, HskTerminatedCode, Logger, LoggerLevels, MonitorScene, SIPStatus, Scene, SdkErrorCode, SessionStatus, SocketStatus, getInstance };