socket io发出失败的回调

fcb*_*ing 9 callback socket.io emit

有没有办法知道socket io发出失败和成功,比如ajax回调方法:onSuccess,onError?对于socket io emit我只发现:

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

只有在服务器发送ack响应时才会调用此回调.但是它不能适用于这种情况:

在向服务器发送消息的那一刻,网络不良或连接丢失,这意味着服务器没有收到此消息,因此不调用客户端回调函数.

我想要的是:

当我调用socket io发出时,如果失败,我想重试3次.

Rya*_*erg 0

我知道这是一篇旧文章,但以防万一有人仍然遇到问题。

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');
});
Run Code Online (Sandbox Code Playgroud)

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