【问题标题】:Integration of node-whatsapi in meteor流星中node-whatsapi的集成
【发布时间】:2015-08-05 11:47:37
【问题描述】:

我希望能够在流星中发现 whatsapi。我正在使用

  • 最新的稳定流星
  • 节点-whatsapi
  • arunoda 的流星黑客: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 代码必须始终在 Fiber 中运行”错误”)

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

救命!

【问题讨论】:

    标签: meteor npm whatsapi


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 2015-11-30
      • 2014-06-16
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多