@@ -150,7 +150,7 @@ export class UA extends EventEmitter {
150150 toString : ( options ?: any ) => string
151151 } ;
152152 public status : UAStatus ;
153- public transport : Transport | undefined ;
153+ public transport : Transport ;
154154 public sessions : { [ id : string ] : InviteClientContext | InviteServerContext } ;
155155 public subscriptions : { [ id : string ] : Subscription } ;
156156 public data : any ;
@@ -261,6 +261,14 @@ export class UA extends EventEmitter {
261261 throw e ;
262262 }
263263
264+ if ( ! this . configuration . transportConstructor ) {
265+ throw new TransportError ( "Transport constructor not set" ) ;
266+ }
267+ this . transport = new this . configuration . transportConstructor (
268+ this . getLogger ( "sip.transport" ) ,
269+ this . configuration . transportOptions
270+ ) ;
271+
264272 const userAgentCoreConfiguration = makeUserAgentCoreConfigurationFromUA ( this ) ;
265273
266274 // The Replaces header contains information used to match an existing
@@ -400,11 +408,9 @@ export class UA extends EventEmitter {
400408 public unregister ( options ?: any ) : this {
401409 this . configuration . register = false ;
402410
403- if ( this . transport ) {
404- this . transport . afterConnected ( ( ) => {
405- this . registerContext . unregister ( options ) ;
406- } ) ;
407- }
411+ this . transport . afterConnected ( ( ) => {
412+ this . registerContext . unregister ( options ) ;
413+ } ) ;
408414
409415 return this ;
410416 }
@@ -432,20 +438,16 @@ export class UA extends EventEmitter {
432438 // Delay sending actual invite until the next 'tick' if we are already
433439 // connected, so that API consumers can register to events fired by the
434440 // the session.
435- if ( this . transport ) {
436- this . transport . afterConnected ( ( ) => {
437- context . invite ( ) ;
438- this . emit ( "inviteSent" , context ) ;
439- } ) ;
440- }
441+ this . transport . afterConnected ( ( ) => {
442+ context . invite ( ) ;
443+ this . emit ( "inviteSent" , context ) ;
444+ } ) ;
441445 return context ;
442446 }
443447
444448 public subscribe ( target : string | URI , event : string , options : any ) : Subscription {
445449 const sub : Subscription = new Subscription ( this , target , event , options ) ;
446- if ( this . transport ) {
447- this . transport . afterConnected ( ( ) => sub . subscribe ( ) ) ;
448- }
450+ this . transport . afterConnected ( ( ) => sub . subscribe ( ) ) ;
449451 return sub ;
450452 }
451453
@@ -462,11 +464,9 @@ export class UA extends EventEmitter {
462464 public publish ( target : string | URI , event : string , body : string , options : any ) : PublishContext {
463465 const pub : PublishContext = new PublishContext ( this , target , event , options ) ;
464466
465- if ( this . transport ) {
466- this . transport . afterConnected ( ( ) => {
467- pub . publish ( body ) ;
468- } ) ;
469- }
467+ this . transport . afterConnected ( ( ) => {
468+ pub . publish ( body ) ;
469+ } ) ;
470470 return pub ;
471471 }
472472
@@ -494,9 +494,7 @@ export class UA extends EventEmitter {
494494 public request ( method : string , target : string | URI , options ?: any ) : ClientContext {
495495 const req : ClientContext = new ClientContext ( this , method , target , options ) ;
496496
497- if ( this . transport ) {
498- this . transport . afterConnected ( ( ) => req . send ( ) ) ;
499- }
497+ this . transport . afterConnected ( ( ) => req . send ( ) ) ;
500498 return req ;
501499 }
502500
@@ -549,9 +547,7 @@ export class UA extends EventEmitter {
549547 this . status = UAStatus . STATUS_USER_CLOSED ;
550548
551549 // Disconnect the transport and reset user agent core
552- if ( this . transport ) {
553- this . transport . disconnect ( ) ;
554- }
550+ this . transport . disconnect ( ) ;
555551 this . userAgentCore . reset ( ) ;
556552
557553 if ( typeof environment . removeEventListener === "function" ) {
@@ -574,24 +570,13 @@ export class UA extends EventEmitter {
574570 this . logger . log ( "user requested startup..." ) ;
575571 if ( this . status === UAStatus . STATUS_INIT ) {
576572 this . status = UAStatus . STATUS_STARTING ;
577- if ( ! this . configuration . transportConstructor ) {
578- throw new TransportError ( "Transport constructor not set" ) ;
579- }
580- this . transport = new this . configuration . transportConstructor (
581- this . getLogger ( "sip.transport" ) ,
582- this . configuration . transportOptions
583- ) ;
584573 this . setTransportListeners ( ) ;
585574 this . emit ( "transportCreated" , this . transport ) ;
586575 this . transport . connect ( ) ;
587-
588576 } else if ( this . status === UAStatus . STATUS_USER_CLOSED ) {
589577 this . logger . log ( "resuming" ) ;
590578 this . status = UAStatus . STATUS_READY ;
591- if ( this . transport ) {
592- this . transport . connect ( ) ;
593- }
594-
579+ this . transport . connect ( ) ;
595580 } else if ( this . status === UAStatus . STATUS_STARTING ) {
596581 this . logger . log ( "UA is in STARTING status, not opening new connection" ) ;
597582 } else if ( this . status === UAStatus . STATUS_READY ) {
@@ -701,11 +686,9 @@ export class UA extends EventEmitter {
701686 * Helper function. Sets transport listeners
702687 */
703688 private setTransportListeners ( ) : void {
704- if ( this . transport ) {
705- this . transport . on ( "connected" , ( ) => this . onTransportConnected ( ) ) ;
706- this . transport . on ( "message" , ( message : string ) => this . onTransportReceiveMsg ( message ) ) ;
707- this . transport . on ( "transportError" , ( ) => this . onTransportError ( ) ) ;
708- }
689+ this . transport . on ( "connected" , ( ) => this . onTransportConnected ( ) ) ;
690+ this . transport . on ( "message" , ( message : string ) => this . onTransportReceiveMsg ( message ) ) ;
691+ this . transport . on ( "transportError" , ( ) => this . onTransportError ( ) ) ;
709692 }
710693
711694 /**
@@ -876,6 +859,8 @@ export class UA extends EventEmitter {
876859 transportConstructor : WebTransport ,
877860 transportOptions : { } ,
878861
862+ usePreloadedRoute : false ,
863+
879864 // string to be inserted into User-Agent request header
880865 userAgentString : SIPConstants . USER_AGENT ,
881866
@@ -1236,6 +1221,12 @@ export class UA extends EventEmitter {
12361221 }
12371222 } ,
12381223
1224+ usePreloadedRoute : ( usePreloadedRoute : boolean ) : boolean | undefined => {
1225+ if ( typeof usePreloadedRoute === "boolean" ) {
1226+ return usePreloadedRoute ;
1227+ }
1228+ } ,
1229+
12391230 userAgentString : ( userAgentString : string ) : string | undefined => {
12401231 if ( typeof userAgentString === "string" ) {
12411232 return userAgentString ;
@@ -1317,7 +1308,10 @@ export function makeUserAgentCoreConfigurationFromUA(ua: UA): UserAgentCoreConfi
13171308 const contact = ua . contact ;
13181309 const displayName = ua . configuration . displayName ? ua . configuration . displayName : "" ;
13191310 const hackViaTcp = ua . configuration . hackViaTcp ? true : false ;
1320- const routeSet = ua . configuration . usePreloadedRoute && ua . transport ? [ ua . transport . server . sipUri ] : [ ] ;
1311+ const routeSet =
1312+ ua . configuration . usePreloadedRoute && ua . transport . server && ua . transport . server . sipUri ?
1313+ [ ua . transport . server . sipUri ] :
1314+ [ ] ;
13211315 const sipjsId = ua . configuration . sipjsId || Utils . createRandomToken ( 5 ) ;
13221316
13231317 let supportedOptionTags : Array < string > = [ ] ;
0 commit comments