@@ -368,6 +368,7 @@ export interface DirectLineOptions {
368368 webSocket ?: boolean ,
369369 pollingInterval ?: number ,
370370 streamUrl ?: string ,
371+ timeout ?: number ,
371372 // Attached to all requests to identify requesting agent.
372373 botAgent ?: string
373374}
@@ -425,8 +426,6 @@ const makeServices = (services: Partial<Services>): Services => {
425426
426427const lifetimeRefreshToken = 30 * 60 * 1000 ;
427428const intervalRefreshToken = lifetimeRefreshToken / 2 ;
428- const timeout = 20 * 1000 ;
429- const retries = ( lifetimeRefreshToken - intervalRefreshToken ) / timeout ;
430429
431430const POLLING_INTERVAL_LOWER_BOUND : number = 200 ; //ms
432431
@@ -468,6 +467,8 @@ export class DirectLine implements IBotConnection {
468467 private _userAgent : string ;
469468 public referenceGrammarId : string ;
470469 private botIdHeader : string ;
470+ private timeout = 20 * 1000 ;
471+ private retries : number ;
471472
472473 private pollingInterval : number = 1000 ; //ms
473474
@@ -498,6 +499,12 @@ export class DirectLine implements IBotConnection {
498499 }
499500 }
500501
502+ if ( options . timeout !== undefined ) {
503+ this . timeout = options . timeout ;
504+ }
505+
506+ this . retries = ( lifetimeRefreshToken - intervalRefreshToken ) / this . timeout ;
507+
501508 this . _botAgent = this . getBotAgent ( options . botAgent ) ;
502509
503510 this . services = makeServices ( options ) ;
@@ -613,7 +620,7 @@ export class DirectLine implements IBotConnection {
613620 return this . services . ajax ( {
614621 method,
615622 url,
616- timeout,
623+ timeout : this . timeout ,
617624 headers : {
618625 "Accept" : "application/json" ,
619626 ...this . commonHeaders ( )
@@ -637,8 +644,8 @@ export class DirectLine implements IBotConnection {
637644 ? Observable . throw ( error , this . services . scheduler )
638645 : Observable . of ( error , this . services . scheduler )
639646 } )
640- . delay ( timeout , this . services . scheduler )
641- . take ( retries )
647+ . delay ( this . timeout , this . services . scheduler )
648+ . take ( this . retries )
642649 )
643650 }
644651
@@ -657,7 +664,7 @@ export class DirectLine implements IBotConnection {
657664 this . services . ajax ( {
658665 method : "POST" ,
659666 url : `${ this . domain } /tokens/refresh` ,
660- timeout,
667+ timeout : this . timeout ,
661668 headers : {
662669 ...this . commonHeaders ( )
663670 }
@@ -676,8 +683,8 @@ export class DirectLine implements IBotConnection {
676683
677684 return Observable . of ( error , this . services . scheduler ) ;
678685 } )
679- . delay ( timeout , this . services . scheduler )
680- . take ( retries )
686+ . delay ( this . timeout , this . services . scheduler )
687+ . take ( this . retries )
681688 )
682689 )
683690 }
@@ -711,7 +718,7 @@ export class DirectLine implements IBotConnection {
711718 method : "GET" ,
712719 url : `${ this . domain } /session/getsessionid` ,
713720 withCredentials : true ,
714- timeout,
721+ timeout : this . timeout ,
715722 headers : {
716723 "Content-Type" : "application/json" ,
717724 ...this . commonHeaders ( )
@@ -748,7 +755,7 @@ export class DirectLine implements IBotConnection {
748755 method : "POST" ,
749756 url : `${ this . domain } /conversations/${ this . conversationId } /activities` ,
750757 body : activity ,
751- timeout,
758+ timeout : this . timeout ,
752759 headers : {
753760 "Content-Type" : "application/json" ,
754761 ...this . commonHeaders ( )
@@ -802,7 +809,7 @@ export class DirectLine implements IBotConnection {
802809 method : "POST" ,
803810 url : `${ this . domain } /conversations/${ this . conversationId } /upload?userId=${ message . from . id } ` ,
804811 body : formData ,
805- timeout,
812+ timeout : this . timeout ,
806813 headers : {
807814 ...this . commonHeaders ( )
808815 }
@@ -848,7 +855,7 @@ export class DirectLine implements IBotConnection {
848855 } ,
849856 method : 'GET' ,
850857 url : `${ this . domain } /conversations/${ this . conversationId } /activities?watermark=${ this . watermark } ` ,
851- timeout
858+ timeout : this . timeout
852859 } ) . subscribe (
853860 ( result : AjaxResponse ) => {
854861 subscriber . next ( result ) ;
@@ -921,7 +928,7 @@ export class DirectLine implements IBotConnection {
921928 // If we periodically ping the server with empty messages, it helps Chrome
922929 // realize when connection breaks, and close the socket. We then throw an
923930 // error, and that give us the opportunity to attempt to reconnect.
924- sub = Observable . interval ( timeout , this . services . scheduler ) . subscribe ( _ => {
931+ sub = Observable . interval ( this . timeout , this . services . scheduler ) . subscribe ( _ => {
925932 try {
926933 ws . send ( "" )
927934 } catch ( e ) {
@@ -954,7 +961,7 @@ export class DirectLine implements IBotConnection {
954961 this . services . ajax ( {
955962 method : "GET" ,
956963 url : `${ this . domain } /conversations/${ this . conversationId } ?watermark=${ this . watermark } ` ,
957- timeout,
964+ timeout : this . timeout ,
958965 headers : {
959966 "Accept" : "application/json" ,
960967 ...this . commonHeaders ( )
@@ -978,8 +985,8 @@ export class DirectLine implements IBotConnection {
978985
979986 return Observable . of ( error , this . services . scheduler ) ;
980987 } )
981- . delay ( timeout , this . services . scheduler )
982- . take ( retries )
988+ . delay ( this . timeout , this . services . scheduler )
989+ . take ( this . retries )
983990 )
984991 )
985992 }
0 commit comments