设置流星重新连接时间

时间:2015-03-10 17:56:31

标签: javascript meteor

我的meteor应用程序的客户端尝试以增加的时间间隔重新连接到服务器。使用((Meteor.status().retryTime - (new Date()).getTime())/1000).toFixed(0),我粗略估计重新连接间隔是第1次:1秒,第2次:2秒,第3次:4秒,第4次:12秒,第5次:18秒,第6次:62秒,第7次:108秒。有没有办法设置间隔长度?例如,无论我尝试重新连接多少次,我每次都可以将重新连接间隔设置为5秒吗?

1 个答案:

答案 0 :(得分:8)


<强>更新: 我构建了一个包来实现这个功能 - nspangler:autoreconnect


我的最终解决方案是跟踪Meteor.status()并在状态为waiting时构建自定义时间间隔。这是客户端上的代码。

 // Variable For Storing Interval ID
 var intervalId = null;

 Meteor.startup( function () {

  // Interval Reconnect
  Tracker.autorun( function () {
    // Start Pinging For Recconect On Interval, only if status is faiting and intervalId is null
    if(Meteor.status().status === "waiting" && intervalId === null) {
        intervalId = Meteor.setInterval( function () {
            console.log("attempt to reconnect");
            Meteor.reconnect()
        }, 1000);
        console.log(intervalId);
    }
    // Stop Trying to Reconnect If Connected, and clear Interval
    if(Meteor.status().status === "connected" && intervalId != null) {
        console.log("cleared interval");
        Meteor.clearInterval(intervalId);
        intervalId = null;
    }
  })

});
相关问题