meteor,错误:无法等服务器上使用Meteor.setTimeout()光纤

我对meteor相当陌生,所以我可能会错过这里的一个关键洞察。

无论如何,我想向用户指出同时有多less其他用户在网站上。 我有一个AuditItems集合,它已经存储了kv-pairs对于whowhat when以及我可以用于这种types的计算。 我的Collections查询运行一个new Date()参数,所以我不能只是观察结果,我必须定期重新运行查询。

然后,我通过调用Deps.Dependency changed Deps.Dependency

这里是代码:

  // publishing counts of the users that are logged on // at the moment Meteor.publish("user-activity", function () { var self = this; self.added("user-activity", "all-recent-activity", {'operations': getRecentActivityCountsCache}); self.ready(); Deps.autorun(function() { self.changed("user-activity", "all-recent-activity", {'operations': getRecentActivityCounts()}); }); }); var getRecentActiveCountsDependency = new Deps.Dependency; var getRecentActivityCountsCache = 0; var getRecentActivityCounts = function() { // register dependency with the caller getRecentActiveCountsDependency.depend(); var now = new Date(); var aWhileAgo = new Date(now.valueOf() - (5 * 60 * 1000)); // 5 minutes auditItems = AuditItems.find({'when': { '$gt': aWhileAgo }}).fetch(); console.log('raw data: ' + JSON.stringify(auditItems)); getRecentActivityCountsCache = _.chain(auditItems) .groupBy('who') .keys() .size() .value(); console.log('new count: ' + getRecentActivityCountsCache); return getRecentActivityCountsCache; }; Meteor.setTimeout(function() { getRecentActiveCountsDependency.changed(); }, 60 * 1000); // 60 seconds 

定时器第一次触发时,控制台上出现这个错误:

  Exception from Deps recompute: Error: Can't wait without a fiber at Function.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:83:9) at Object.Future.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:325:10) at _.extend._nextObject (packages/mongo-livedata/mongo_driver.js:540) at _.extend.forEach (packages/mongo-livedata/mongo_driver.js:570) at _.extend.map (packages/mongo-livedata/mongo_driver.js:582) at _.extend.fetch (packages/mongo-livedata/mongo_driver.js:606) at _.each.Cursor.(anonymous function) [as fetch] (packages/mongo-livedata/mongo_driver.js:444) at getRecentActivityCounts (app/server/server.js:26:70) at null._func (app/server/server.js:12:79) at _.extend._compute (packages/deps/deps.js:129) 

所有Deps方法只能在客户端上使用 。 你得到的错误是Deps被build立的方式(因为客户端不使用光纤)。

如果你想在服务器上有反应,你需要使用observeobserve observeChanges 。 使用添加和删除的句柄,您可以查询该点的date(文档更改时)。

您也可以使用Meteor.setInterval定期从那里删除旧用户。

你可以做的一件事就是使用一个像meteor一样的软件包来完成这一切。 它的function是拥有一个包含每个在线人员信息的实时集合,然后当他们离线/超时后,使用定期的Meteor.setInterval方法将其从集合中删除。