1

当在指定端口上接收到 ZeroMQ 消息时,以下 Meteor 代码片段会停止。但是,如果我更改removefind,它可以正常工作。 insert操作也失败。请注意,insert如果remove在回调之外运行,例如在部件下方,则成功pull.on

var Components = new Meteor.Collection("components");

function handle_message(msg) {
    console.log("pull on message" + msg);
    Components.remove();
}


if (Meteor.isServer) {
     Meteor.startup(function () {
        var zmq = Meteor.require("zmq");
        var pull = zmq.socket("pull");
        pull.bind("tcp://127.0.0.1:7000", function(data) {
           console.log("Connection received from ZMQ");
        });
        pull.on('message', handle_message);

    });
}

例外是:

W20130827-21:36:21.800(0)? (STDERR) packages/mongo-livedata.js:1640
W20130827-21:36:21.802(0)? (STDERR)         throw e;                                                              
W20130827-21:36:21.803(0)? (STDERR)               ^
W20130827-21:36:21.843(0)? (STDERR) TypeError: Cannot read property '_meteor_dynamics' of undefined
W20130827-21:36:21.843(0)? (STDERR)     at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:55)
W20130827-21:36:21.845(0)? (STDERR)     at _.extend._wrapAsync (packages/meteor/helpers.js:108)
W20130827-21:36:21.845(0)? (STDERR)     at _.each.MongoConnection.(anonymous function) [as remove] (packages/mongo-livedata/mongo_driver.js:340)
W20130827-21:36:21.846(0)? (STDERR)     at _.each.Meteor.Collection.(anonymous function) [as remove] (packages/mongo-livedata/collection.js:406)
W20130827-21:36:21.846(0)? (STDERR)     at Socket.handle_message (app/web.js:5:16)
W20130827-21:36:21.847(0)? (STDERR)     at Socket.EventEmitter.emit (events.js:96:17)
W20130827-21:36:21.847(0)? (STDERR)     at Socket._flush._flushing (/home/vagrant/.meteorite/packages/npm/arunoda/meteor-npm/ad83acff83385d5ea05997c8bbc2d7493ba4c04e/.build/npm/node_modules/zmq/lib/index.js:358:25)
W20130827-21:36:21.852(0)? (STDERR)     at global.setImmediate (/home/vagrant/.meteorite/packages/npm/arunoda/meteor-npm/ad83acff83385d5ea05997c8bbc2d7493ba4c04e/.build/npm/node_modules/zmq/node_modules/set-immediate/setImmediate.js:15:9)
W20130827-21:36:21.852(0)? (STDERR)     at process.startup.processNextTick.process._tickCallback (node.js:245:9)

有人有想法么?谢谢!

4

1 回答 1

1

我在这里找到了解决问题的提示(尽管我还没有声称完全理解它)。包装回调Meteor.bindEnvironment可以消除错误removeinsert正确处理。作为流星新手,我很乐意接受另一个答案,它很好地解释了为什么/如何工作。

var Components = new Meteor.Collection("components");

function handle_message(msg) {
    console.log("pull on message" + msg);
    Components.insert({"name": "foo", "state": "stopped"});
}

bound_handle_message = Meteor.bindEnvironment(handle_message, function(e) {
    console.log("exception! " + e); 
});

if (Meteor.isServer) {
     Meteor.startup(function () {
        var zmq = Meteor.require("zmq");
        var pull = zmq.socket("pull");
        pull.bind("tcp://127.0.0.1:7000", function(data) {
           console.log("Connection received from ZMQ");
        });
        pull.on('message', bound_handle_message);

    });
}
于 2013-08-28T00:18:23.110 回答