0

我正在尝试 Meteor 并发现了一些奇怪的东西。我正在用他们的文档中提到的这个例子做实验(我已经修改了一点):

if (Meteor.isClient) {

  Counts = new Meteor.Collection("counts");

    // client: subscribe to the count for the current room
    Meteor.subscribe("counts-by-room", '1');

}

if (Meteor.isServer) {

  Messages = new Meteor.Collection("messages");

  Meteor.publish("counts-by-room", function (roomId) {
      var self = this;
      check(roomId, String);
      var count = 0;
      var initializing = true;
      var handle = Messages.find({roomId: roomId}).observeChanges({
       added: function (id) {
          count++;
          if (!initializing)
            self.changed("counts", roomId, {count: count});
        },
        removed: function (id) {
          count--;
          self.changed("counts", roomId, {count: count});
        }
        // don't care about moved or changed
      });



      // Observe only returns after the initial added callbacks have
      // run.  Now return an initial value and mark the subscription
      // as ready.
      initializing = false;
      self.added("counts", roomId, {count: count});
      self.ready();

      console.log("opened new handle");

      // Stop observing the cursor when client unsubs.
      // Stopping a subscription automatically takes
      // care of sending the client any removed messages.
      self.onStop(function () {
        console.log("stopping");
        handle.stop();
      });
    });
}

然后我使用 Chrome Dev Console 多次订阅同一个房间,在我的服务器控制台上我可以看到它每次都打开新的句柄。服务器控制台:

I20130815-14:15:07.470(5.5)? opened new handle
I20130815-14:15:37.661(5.5)? opened new handle
I20130815-14:15:38.616(5.5)? opened new handle
I20130815-14:15:39.191(5.5)? opened new handle
I20130815-14:15:39.703(5.5)? opened new handle
I20130815-14:15:40.215(5.5)? opened new handle
I20130815-14:15:40.711(5.5)? opened new handle
I20130815-14:15:41.207(5.5)? opened new handle
I20130815-14:15:41.704(5.5)? opened new handle
I20130815-14:15:42.200(5.5)? opened new handle
I20130815-14:15:42.696(5.5)? opened new handle

我想知道这是否正常,因为在他们的文档中他们说阻止观察者非常重要。当有人故意尝试这样做时,我该如何阻止它。我将此问题视为某种内存泄漏,可能会使我的服务器停机。我错了吗?

请帮忙。我是流星的新手:)

4

1 回答 1

0

你不必担心。无论您是否使用 Meteor,任何足够邪恶的人都可以向托管您网站的服务器发送许多请求并使其变慢。你对此无能为力。但是大多数用户并不邪恶,并且确实以您参与的方式使用您的网站,因此如果您编写的代码不会使服务器变慢,那么您在树敌之前是非常安全的。

不减慢服务器速度的一种方法是关闭用户不再需要的订阅句柄。

于 2013-08-15T11:58:02.343 回答