【问题标题】:Meteor - broken communication after ddp reconnectMeteor - ddp重新连接后通信中断
【发布时间】:2014-10-05 13:35:40
【问题描述】:

我正在编写由两个模块组成的应用程序 - 客户端和服务器。服务器发布记录集和函数,客户端订阅记录集并调用远程函数。两个模块都运行服务器端。在重新连接 ddp 会话(即服务器重新启动)之前,一切都按预期工作。 重新连接后,远程函数调用停止返回任何值,订阅也中断(无事件)。

我能够找到两个同时使用的操作会导致这种效果。 它的“self.ready()”并在重新连接处理程序中调用任何远程函数。 如果我删除其中任何一个,那么一切都会恢复正常。

服务器:

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-subscribe serverside-javascript ddp


    【解决方案1】:

    您需要清理现有的发布方法,否则它会在您断开连接时尝试发布到不存在的订阅时崩溃。

    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 啊,我明白了。不幸的是,我认为我不知道在那种情况下发生了什么。
    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多