设置流星重新连接时间

Set Meteor Reconnect Time

本文关键字:连接时间 流星 设置      更新时间:2023-09-26

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


更新:我构建了一个软件包来实现此功能 - 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;
    }
  })
});