Skip to content

Commit 69d9344

Browse files
juharrisJustin Harris
andauthored
DirectLine: Add timeout option (#277)
* DirectLine: Add timeout option * README: Add docs for timeout. Co-authored-by: Justin Harris <justin.harris@microsoft.com>
1 parent 2a17cd4 commit 69d9344

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ var directLine = new DirectLine({
7777
token: /* or put your Direct Line token here (supply secret OR token, not both) */,
7878
domain: /* optional: if you are not using the default Direct Line endpoint, e.g. if you are using a region-specific endpoint, put its full URL here */
7979
webSocket: /* optional: false if you want to use polling GET to receive messages. Defaults to true (use WebSocket). */,
80-
pollingInterval: /* optional: set polling interval in milliseconds. Default to 1000 */,
80+
pollingInterval: /* optional: set polling interval in milliseconds. Defaults to 1000 */,
81+
timeout: /* optional: a timeout in milliseconds for requests to the bot. Defaults to 20000 */,
8182
});
8283
```
8384

src/directLine.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

426427
const lifetimeRefreshToken = 30 * 60 * 1000;
427428
const intervalRefreshToken = lifetimeRefreshToken / 2;
428-
const timeout = 20 * 1000;
429-
const retries = (lifetimeRefreshToken - intervalRefreshToken) / timeout;
430429

431430
const 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

Comments
 (0)