回复您的最后一条评论@Lior。如果没有收到 ACK,将执行“catch”块。如何正确重试=我不知道。我不是 SocketCluster 专家,但似乎没有自动机制来处理这个问题。您可以在 try / catch 块周围放置一个“do / while”循环(通过防止无限循环):
let i = 0;
let callIsSuccessful = false;
do {
try {
// Publish data; wait for an acknowledgement from the back end broker (if it exists).
await agServer.exchange.invokePublish('foo', 'This is some more data');
callIsSuccessful = true;
} catch (error) {
// ... Handle potential error if broker does not acknowledge before timeout.
}
} while (i++ < MAX_RETRIES && (!callIsSuccessful))
您还可以通过递归方式调用执行发布操作的函数:
callingFunction(paramData, currentRetryNumber){
if(currentRetryNumber >= MAX_RETRIES){
return;
}
try {
// Publish data; wait for an acknowledgement from the back end broker (if it exists).
await agServer.exchange.invokePublish('foo', 'This is some more data');
} catch (error) {
callingFunction(paramData, ++currentRetryNumber);
// ... Handle potential error if broker does not acknowledge before timeout.
}
}
(async () => {
callingFunction(paramData, 0);
})();
满意吗?;)