gpt4 book ai didi

callback - 套接字io发出失败的回调

转载 作者:行者123 更新时间:2023-12-03 16:28:32 27 4
gpt4 key购买 nike

有没有办法知道套接字io发出失败和成功,就像ajax回调方法一样:onSuccess,onError?
对于io套接字,我只能找到:

socket.emit('publish', {message:'test message'},function (data) { alert("")})



仅当服务器发送ack响应时才调用此回调。但这不适用于这种情况:

At the moment of emit message to server, there is bad network or lost connection, that means server not receive this message, so the client callback function is not called.



我想要的是:

When I call the socket io emit, if it fails, I want to retry 3 times.

最佳答案

我知道这是一篇过时的文章,但是以防万一有人仍然遇到麻烦。

var socket = new io.connect('http://localhost:3000', {
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionDelayMax' : 5000,
'reconnectionAttempts': 3
});
socket.on('connect_error', function() {
console.log('Connection failed');
});
socket.on('reconnect_failed', function() {
// fired only after the 3 attemps in this example fail
console.log('Reconnection failed');
});

更多信息在这里-> https://socket.io/docs/client-api/#manager-reconnectionAttempts-value

关于callback - 套接字io发出失败的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com