hs-cti.es5.umd.js 205 KB

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