socket io发出失败的回调

时间:2015-12-25 02:31:49

标签: callback socket.io emit

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

  

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

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

  

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

我想要的是:

  

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

1 个答案:

答案 0 :(得分: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');
});

此处有更多信息-> https://socket.io/docs/client-api/#manager-reconnectionAttempts-value