0

I'm writing application consisting of two modules - client and server. Server publishes recordset and function, client subscribes on recordset and calls remote functions. Both modules run server side. Everything works as expected until the reconnection of ddp session (i.e server restart). After the reconnect, remote function calls cease to return any values, subscription is also broken (no events).

I was able to find two operations which used simultaneously cause this effect. Its "self.ready()" and calling any remote function inside reconnect handler. If I remove any of that then everything go backs to normal.

Server:

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';
    }
  });
}

Client:

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);
}

So the question is: What causes such a behavior?

4

1 回答 1

0

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

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);
    });
});

您还应该(可能)在服务器控制台中看到一些有关它的调试/有用信息

于 2014-10-05T15:00:43.517 回答