【问题标题】:How to use a client side reconnection event in Meteor如何在 Meteor 中使用客户端重新连接事件
【发布时间】:2013-01-04 00:02:06
【问题描述】:

如何在 Meteor 中使用客户端重新连接事件。

在客户端,Meteor.apply 采用新的等待选项,确保 在此方法之前不会向服务器发送进一步的方法调用 完成;它用于登录和注销方法,以保持 用户 ID 定义明确。您还可以指定一个 onReconnect 处理程序 在重新建立连接时运行;流星帐户使用 这将在重新连接时重新登录。

谁能举个例子。

这是帐户包中的示例。

  Accounts._makeClientLoggedIn = function(userId, token) {
    Accounts._storeLoginToken(userId, token);
    Meteor.default_connection.setUserId(userId);
    Meteor.default_connection.onReconnect = function() {
      Meteor.apply('login', [{resume: token}], {wait: true}, function(error, result) {
        if (error) {
          Accounts._makeClientLoggedOut();
          throw error;
        } else {
          // nothing to do
        }
      });
    };
    userLoadedListeners.invalidateAll();
    if (currentUserSubscriptionData) {
      currentUserSubscriptionData.handle.stop();
    }
    var data = currentUserSubscriptionData = {loaded: false};
    data.handle = Meteor.subscribe(
      "meteor.currentUser", function () {
        // Important! We use "data" here, not "currentUserSubscriptionData", so
        // that if we log out and in again before this subscription is ready, we
        // don't make currentUserSubscriptionData look ready just because this
        // older iteration of subscribing is ready.
        data.loaded = true;
        userLoadedListeners.invalidateAll();
      });
  };

如果您希望帐户仍然有效,我假设您不能只定义另一个 default_connection.onReconnect?

谢谢。

编辑

再想一想,您是否应该使用 Meteor.status() 而不是使用 onReconnect?

【问题讨论】:

  • 你想做什么?
  • @Rahul 在重新连接时运行一些东西。
  • @Harry——你是对的。我对此进行了测试并编辑了我的答案。 Meteor.status 绝对是确定您的客户端是否断开连接的正确方法。

标签: meteor reconnect


【解决方案1】:

Harry,我在上面看到了您的评论并进行了更改。我认为你是对的。因为Meteor.status 是一个反应变量,所以它会在连接状态发生变化时重新运行。

if (Meteor.isClient) {
    Tracker.autorun(function () {
        if (Meteor.status().connected) {
            console.log("connected");
        } else {
            console.log("disconnected");
        }
    });
}

【讨论】:

  • 感谢您的回答。如果我们想为未登录的用户使用它怎么办?
  • 我在上面的autorun 块中添加了一个else,这会触发。虽然我不是 100% 确定这是你想要的。您是否尝试连接一个应该在断开连接后重新建立网络连接时触发的功能?
  • 谢谢!很有帮助。虽然,在最新版本的流星中,我认为是Tracker.autorun
  • 当然! Meteor 开发进展如此之快,这个答案落后了两次迭代 :) [Meteor.autorun -> Deps.autorun -> Tracker.autorun]
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 2021-09-27
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多