【发布时间】: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 绝对是确定您的客户端是否断开连接的正确方法。