hs-cti.es6.esm.js 148 KB

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