在Meteor for CloudMQTT中配置mqttConnect选项

时间:2016-02-25 07:52:12

标签: javascript node.js meteor mqtt

我尝试使用Meteor软件包perak:meteor-mqtt-collection连接到CloudMQTT,但我不确定如何解释mqttConnect function的语法:

Collection.mqttConnect(uri, topics, options, mqttOptions)
  

"其中mqttOptions是在MQTT.js库中提供给mqtt.connect([url],options)的对象,用于配置MQTT.js-client的底层选项。 See the docs"

到目前为止,我的Meteor测试代码如下所示:

Goals = new Meteor.Collection('dbGoals');

if (Meteor.isClient) {
  Goals.insert({ 
    topic: "goals", 
    message: "Hello world from Meteor Web Client", 
    broadcast: true 
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    Goals.mqttConnect("m10.cloudmqtt.com", ["goals"], {
      insert: true,
      raw: true
    },
    { servers: [{ host: 'm10.cloudmqtt.com', port: 12310 }],
      clientId:"uniqueIdforEachMqttClient",
      username: "myMqttUserName",
      password: "myMqttUserPass",
      clean:false
    });

并收到以下错误:

C:\Users\user\AppData\Local\.meteor\packages\meteor-tool\1.1.10\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:245
                                            throw(ex);
                                                  ^
TypeError: Cannot call method 'replace' of null
at Object.connect (C:\Users\user\AppData\Local\.meteor\packages\perak_mqtt-collection\1.0.4\npm\node_modules\mqtt\lib\connect\index.js:62:35)
at [object Object].Mongo.Collection.mqttConnect (packages/perak_mqtt-collection/packages/perak_mqtt-collection.js:37:1)
at E:\Data\Projects\Project2016\design\sw\mqttColl\.meteor\local\build\programs\server\boot.js:249:5
at mqttColl.js:25:11
=> Exited with code: 8
=> Your application is crashing.

mqttColl.js:25:11行是:

Goals.mqttConnect("m10.cloudmqtt.com", "goals", {

我知道我的servers:选项的对象适用于在Node和CloudMQTT上运行的MQTT.js,但我不确定我是否已获得mqttConnect()功能为perak Meteor软件包正确输入的参数。对于初学者来说,似乎不太可能在多个地方需要MQTT代理的URL,而且主题goals应该放在括号中,但我不清楚参数语法。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您应该在网址中添加协议:mqtt://m10.cloudmqtt.com