Meteor 流星-ddp重新连接后通信中断
我正在编写由两个模块组成的应用程序-客户端和服务器。服务器发布记录集和函数,客户端订阅记录集并调用远程函数。两个模块都运行服务器端。在重新连接ddp会话(即服务器重新启动)之前,一切正常。 重新连接后,远程函数调用停止返回任何值,订阅也会中断(无事件) 我能够找到两个同时使用的操作导致这种效果。 它的“self.ready()”并调用重新连接处理程序中的任何远程函数。 如果我去掉任何一个,那么一切都会恢复正常 服务器:Meteor 流星-ddp重新连接后通信中断,meteor,publish-subscribe,serverside-javascript,ddp,Meteor,Publish Subscribe,Serverside Javascript,Ddp,我正在编写由两个模块组成的应用程序-客户端和服务器。服务器发布记录集和函数,客户端订阅记录集并调用远程函数。两个模块都运行服务器端。在重新连接ddp会话(即服务器重新启动)之前,一切正常。 重新连接后,远程函数调用停止返回任何值,订阅也会中断(无事件) 我能够找到两个同时使用的操作导致这种效果。 它的“self.ready()”并调用重新连接处理程序中的任何远程函数。 如果我去掉任何一个,那么一切都会恢复正常 服务器: if (Meteor.isServer) { Meteor.publis
if (Meteor.isServer) {
Meteor.publish("pname", function () {
var self = this;
var id = Random.id();
self.added("pname", id, {"init": "demo"});
self.ready();
Meteor.setInterval(function(){
var id = Random.id();
self.added("pname", id, {"init": "test"});
self.removed("pname", id);
}, 2000);
});
Meteor.methods({
'demo': function (){
console.log('demo function called');
return 'demo';
}
});
}
客户:
if (Meteor.isServer) {
var remote = DDP.connect("http://example.com:3000");
remote.onReconnect = function() {
console.log('reconnect');
console.log('calling remote function inside reconnect');
var temp = remote.call('demo');
console.log(temp);
};
var collection = new Meteor.Collection("pname", remote);
collection.find({}).observe({
"added": function(item) {
console.log('added', item);
}
});
remote.subscribe("pname");
Meteor.setInterval(function(){
console.log('calling remote function');
var temp = remote.call('demo');
console.log('Result: ' + temp); //after reconnect this line is not called
}, 2000);
}
因此,问题是:
是什么导致了这种行为 您需要清理现有的发布方法,否则当您断开连接时,它将在尝试发布到不存在的订阅时崩溃
Meteor.publish("pname", function () {
var self = this;
var id = Random.id();
self.added("pname", id, {"init": "demo"});
self.ready();
var intervalid = Meteor.setInterval(function(){
var id = Random.id();
self.added("pname", id, {"init": "test"});
self.removed("pname", id);
}, 2000);
self.onStop(function() {
Meteor.clearInterval(intervalid);
});
});
您还应该(可能)在服务器控制台中看到一些关于它的调试/有用信息我不明白,我写道我们可以通过重置(停止/启动)服务器模块来模拟断开连接,因此它不知道以前的订阅。当我添加self.onStop(function(){console.log('onStop publish');});然后,当重置客户端模块时,我可以看到它停止发布。@user4110474啊,我明白了。不幸的是,我不知道在那种情况下发生了什么。