我尝试在NodeJS中将socketCluster与确认一起使用。
我遵循此页面中的代码:https : //socketcluster.io/docs/basic-usage/
[服务器]发布到频道并等待确认
(async () => {
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) {
// ... Handle potential error if broker does not acknowledge before timeout.
}
})();
但是我没有得到认可,也不知道缺少了什么。我尝试使用此选项{ackTimeout:10000}上传套接字集群服务器,但没有帮助。我如何知道是否收到该消息?
回复您对@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);
})();
令人满意吗?;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句