Skip to content

Commit 2cddd67

Browse files
Merge pull request #49 from bobbyngwu/master
Allow for variable timeout
2 parents 99faa4f + 27ee538 commit 2cddd67

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/https.android.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
186186
if (opts.allowLargeResponse) {
187187
android.os.StrictMode.setThreadPolicy(android.os.StrictMode.ThreadPolicy.LAX);
188188
}
189+
190+
//set connection timeout to override okhttp3 default
191+
if (opts.timeout) {
192+
client = client.newBuilder()
193+
.connectTimeout(opts.timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
194+
.writeTimeout(opts.timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
195+
.readTimeout(opts.timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
196+
.build();
197+
}
189198

190199
client.newCall(request.build()).enqueue(new okhttp3.Callback({
191200
onResponse: (task, response) => {

src/https.common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface HttpsRequestOptions {
2323
* Note that once set to true, this policy remains active until the app is killed.
2424
*/
2525
allowLargeResponse?: boolean;
26+
timeout?: number;
2627
}
2728

2829
export interface HttpsResponse {

src/https.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
166166
Object.keys(cont).forEach(key => dict.setValueForKey(cont[key] as any, key));
167167
}
168168
}
169+
170+
if (opts.timeout) {
171+
manager.requestSerializer.timeoutInterval = opts.timeout / 1000;
172+
}
169173

170174
let methods = {
171175
'GET': 'GETParametersSuccessFailure',

0 commit comments

Comments
 (0)