从mqtt客户端browserify建立wss连接

时间:2016-06-14 03:16:13

标签: node.js browserify mqtt wss mosquitto

我按照mqtt网站上的示例编写了一个应用程序,如下所示。

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import {EdgeNode,KSIOTDataType} from './browserEdgeNode';

import './main.html';

Template.hello.onCreated(function helloOnCreated() {
  // counter starts at 0
  debugger;
  this.counter = new ReactiveVar(0);
 var willMessage = {
        topic: 'WillMessage',
        payload: 'This is the last will message',
        qos: 2,
        retain: true
    };

    var mqttOptions2 = {
        clientId: '101',
        protocolId: 'MQIsdp',
        protocolVersion: 3,
        keepAlive: 10000,
        clean: false,
        reconnectPeriod: '1000',
        will: willMessage
    };

    var messageReceivedCallBack = {
        onMessageReceived: function (message) {
            var date = new Date();
            console.log('Message received : ' + message);
        }
    };

    var edgeNode = new EdgeNode('ws://localhost:10001', mqttOptions2, messageReceivedCallBack);

    console.log(EdgeNode.KSIOTDataType);

    var topic = {
      dataTypeID: 'someDataTypeId',
      itemID: 'someItemId',
      itemType: 'someItemType',
      dataType: KSIOTDataType.EVENT,
      qos: 2
    };

     var topicArray = [];
     topicArray[0] = topic;
     edgeNode.subscribe(topicArray);

     var ksiotMessage = {
        ksiotTopic: topic,
        payload: '{"name":"edgeNodeTest"}'
    };

    edgeNode.publish(ksiotMessage, 2);
});

Template.hello.helpers({
  counter() {
    return Template.instance().counter.get();
  },
});

Template.hello.events({
  'click button'(event, instance) {
    // increment the counter when button is clicked
    instance.counter.set(instance.counter.get() + 1);
  },
});

如何通过浏览器建立wss连接?是否可以使用我用于nodeJS的相同mqtt选项,然后浏览并建立一个wss连接?

mqttOptions = {
    clientId: '100',
    key: fs.readFileSync('G:/Projects/test/client.key'),
    cert: fs.readFileSync('G:/Projects/test/client.crt'),
    ca: fs.readFileSync('G:/Projects/test/ca.crt'),
    secureProtocol: 'TLSv1_method',
    rejectUnauthorized: false,
    protocolId: 'MQIsdp',
    protocolVersion: 3,
    passphrase: 'edgenode',
    keepAlive: 10000,
    clean: false,
    reconnectPeriod: '1000',
    will: willMessage
};

0 个答案:

没有答案