节点-whatsapi在流星中的集成

时间:2015-08-05 02:07:24

标签: meteor npm whatsapi

我希望能够在流星中嗅到whatsapi。我正在使用

  • 最新的稳定流星
  • 节点whatsapi
  • arunoda的meteorhacks:npm

并且可以获得过去的基础知识:

在流星服务器启动时,我有:

whatsapi = Meteor.npmRequire('whatsapi');
wa = whatsapi.createAdapter({
    msisdn: '....',
    username: '....',
    password: '....',
    ccode: '....'
});

wa.connect(function connected(err) {
    if (err) {console.log(err); return;}
    console.log('Connected');
    wa.login(logged);
});

function logged(err) {
    if (err) {console.log(err); return;}
    console.log('Logged in');
    wa.sendIsOnline();
};

...让我通过方法调用发送和接收消息

wa.sendMessage(recipient, content, function(err, id) {
    if (err) {console.log(err.message); return;}
    console.log('Server received message %s', id);
});

下面的代码也可以工作,在控制台上记录收到的消息。它位于服务器Meteor.startup:

wa.on('receivedMessage', function(message) {
    console.log("From: " + message.from);
    console.log(message.body);
});

我的问题是,当我尝试将store message.from或message.body添加到集合中时,meteor给我“Meteor代码必须始终在光纤内运行”错误“)

wa.on('receivedMessage', function(message) {
    console.log("From: " + message.from);
    console.log(message.body);
    Recipients.insert({msgfrom: message.from});
});

帮助!

1 个答案:

答案 0 :(得分:2)

使用Meteor.bindEnvironment来包装npm模块发出的任何回调。它将把回调包装成一个' Fiber'所以你可以在里面运行Meteor代码。

例如:

wa.on('receivedMessage', Meteor.bindEnvironment(function(message) {
    console.log("From: " + message.from);
    console.log(message.body);
    Recipients.insert({msgfrom: message.from});
}));

它本质上是将回调中的代码放入光纤。