Javascript 在错误超时之前重新发送HTTP post
我有一个Angular 4项目,它使用http post将命令发送到后端。问题是,有时在后端完全启动并运行之前,可能会发出命令。通常会出现“错误连接超时”,但我们的嵌入式浏览器在出现错误之前(5分钟)会因为任何原因在post上停留很长时间。因为5分钟的等待是不可接受的,所以如果在15-30秒内没有响应,我需要想出一种方法来重新发送我们的http帖子 下面是当前帖子的样子Javascript 在错误超时之前重新发送HTTP post,javascript,angular,http,typescript,post,Javascript,Angular,Http,Typescript,Post,我有一个Angular 4项目,它使用http post将命令发送到后端。问题是,有时在后端完全启动并运行之前,可能会发出命令。通常会出现“错误连接超时”,但我们的嵌入式浏览器在出现错误之前(5分钟)会因为任何原因在post上停留很长时间。因为5分钟的等待是不可接受的,所以如果在15-30秒内没有响应,我需要想出一种方法来重新发送我们的http帖子 下面是当前帖子的样子 this._http.post(this.sockclientURL, body, { headers: headers })
this._http.post(this.sockclientURL, body, { headers: headers })
.subscribe((res) => {
let text = res.text();
if (text.startsWith("ERROR")) {
console.log("Sockclient Error.");
if (this.sockclientErrorRetryCount < this.sockclientErrorRetryLimit) {
console.log("Retrying in 3 seconds.");
this.sockclientErrorRetryCount++;
setTimeout(() => {
this.SendCommand(command, success, fail);
}, 3000);
}
return;
}
else {
this.sockclientErrorRetryCount = 0;
}
if (text == "N" || text.startsWith("N ")) {
this._modalService.alert(this._nackLookup.convert(text));
if (typeof fail == 'function') {
fail(text);
}
}
else {
let deserializedCommand = command.deserialize(text);
success(deserializedCommand);
let repeatMillis: number = deserializedCommand.getRepeatMillis();
if (repeatMillis && repeatMillis > 0) {
setTimeout(() => {
this.SendCommand(command, success, fail);
}, repeatMillis);
}
}
},
(err) => {
console.log(err);
let repeatMillis = 1000;
setTimeout(() => {
this.SendCommand(command, success, fail);
}, repeatMillis);
});
this.\u http.post(this.sockclientURL,body,{headers:headers})
.订阅((res)=>{
设text=res.text();
if(text.startsWith(“错误”)){
log(“Sockclient错误”);
if(this.sockclientErrorRetryCount{
this.SendCommand(命令、成功、失败);
}, 3000);
}
返回;
}
否则{
this.sockclientErrorRetryCount=0;
}
if(text==“N”| | text.startsWith(“N”)){
this._modalService.alert(this._nackLookup.convert(text));
if(typeof fail==“函数”){
失败(文本);
}
}
否则{
让deserializedCommand=command.deserialize(文本);
成功(反序列化命令);
让repeatMillis:number=deserializedCommand.getRepeatMillis();
if(repeatMillis&&repeatMillis>0){
设置超时(()=>{
this.SendCommand(命令、成功、失败);
},重复毫秒);
}
}
},
(错误)=>{
控制台日志(err);
设重复毫秒=1000;
设置超时(()=>{
this.SendCommand(命令、成功、失败);
},重复毫秒);
});
因此,为了重新设置上限,我准备了一些代码,以便在发生错误时重新尝试该命令,但我们的嵌入式浏览器会将超时错误保留几分钟。我需要一些东西在15-30秒无响应后尝试重新发送不100%确定它是否仍能在Angular4中工作,但您应该能够做到:
this
._http
.post(this.sockclientURL, body, { headers: headers })
.timeout(15000, new Error('timeout exceeded')) // or 30000
.subscribe((res) => { /* ... */ })
不是100%确定它在Angular4中仍然有效,但您应该能够做到:
this
._http
.post(this.sockclientURL, body, { headers: headers })
.timeout(15000, new Error('timeout exceeded')) // or 30000
.subscribe((res) => { /* ... */ })
重试将立即执行,无需等待延迟。更好的方法是在重试前等待一段时间,然后在给定的时间后中止。Observable允许混合使用retryWhen、delay和timeout操作符来实现这一点,如以下代码段所述:
this._http.post(this.sockclientURL, body, { headers: headers })
.retryWhen(error => error.delay(500))
.timeout(2000, new Error('delay exceeded'))
.map(res => res.map());
重试将立即执行,无需等待延迟。更好的方法是在重试前等待一段时间,然后在给定的时间后中止。Observable允许混合使用retryWhen、delay和timeout操作符来实现这一点,如以下代码段所述:
this._http.post(this.sockclientURL, body, { headers: headers })
.retryWhen(error => error.delay(500))
.timeout(2000, new Error('delay exceeded'))
.map(res => res.map());
关于整个应用范围,还需要更多的信息,但是当我遇到这种情况时,我通常会考虑以下方法:
你能提供更多关于操作范围的信息吗?需要更多关于整个应用范围的信息,但是当我遇到这种情况时,我通常会考虑以下方法:
你能提供更多关于操作范围的信息吗?我得到:类型为“Error”的参数不能分配给类型为“isScheduler”的参数。类型“Error”中缺少属性“now”。在新错误上,看起来他们已经修复了它。timeoutWith(2000,Observable.throw(新错误('delay Extered'))我得到:类型为'Error'的参数不能分配给类型为'IsScheduler'的参数。类型“Error”中缺少属性“now”。在新错误上,看起来他们已经修复了它。timeoutWith(2000,Observable.throw(新错误('delay extended')))我得到的是'Error'类型的参数不能分配给'isScheduler'类型的参数。类型“Error”中缺少属性“now”。看起来我们使用的是.timeoutWith(2000,Observable.throw(新错误('delay Extered')))我得到的是类型为“Error”的参数不能分配给类型为“IsScheduler”的参数。类型“Error”中缺少属性“now”。看起来我们使用的是.timeoutWith(2000,Observable.throw(新错误('delay extended'))