我正在尝试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开发控制台多次订阅同一个房间,在我的服务器控制台上,我可以看到它每次都在打开新的句柄。服务器控制台:
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
我想知道这是否正常,因为在他们的文件中,他们说阻止观察者是非常关键的。当有人故意这样做的时候,我怎么能阻止它呢。我认为这个问题是某种内存泄漏,可能会导致我的服务器停机。我错了吗?
请帮忙。我是流星的新手:)
您不需要担心。无论你是否使用Meteor,任何足够邪恶的人都可以向托管你网站的服务器发送许多请求,并使其速度变慢。你对此无能为力。但大多数用户并不是邪恶的,他们确实以你参与的方式使用你的网站,所以如果你写的代码不会让服务器变慢,那么在你树敌之前,你是非常安全的。
不降低服务器速度的一种方法是关闭订阅。用户不再需要订阅句柄。