1.在https://github.com/eclipse/paho.mqtt.javascript下git clone,下载的文件插入工程

2.

​
import Paho from './paho-mqtt.js'

export default {
  name: 'Controller',
  data () {
    return {
      client: new Paho.Client('127.0.0.1', 8083, ''),
      topic: 'topic'
    }
  },
  methods: {
    onConnectedLost : function(responseObject){  
      console.log("onConnectionLost:"+responseObject.errorMessage);
    },
    onMessageArrived : function (message) {
      console.log("onMessageArrived:"+message.payloadString);
    },
    onConnect:function onConnect() {
      // Once a connection has been made, make a subscription and send a message.
      console.log("onConnect");
      this.client.subscribe(this.topic);
      // message = new Paho.MQTT.Message("Hello");
      // message.destinationName = "World";
      // client.send(message);
    },
  },
  created: function() {
    this.client.connect({
      onSuccess: this.onConnect,
      userName: 'user',
      password: 'pass',
      useSSL:true//允许加密
    });
    this.client.onConnectedLost = this.onConnectedLost;
    this.client.onMessageArrived = this.onMessageArrived;
  }
}

​

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐