Vue——客户端使用MQTT

1.安装依赖

npm i -S mqtt

2.引入依赖

import mqtt from "mqtt/dist/mqtt"

3.主题模式

import mqtt from "mqtt/dist/mqtt"
import baseMQtt, { MqttClient } from "mqtt"

/* 连接mqtt */
const client: MqttClient = mqtt.connect("ws://******/mqtt", {
  username: "admin",
  password: "public1"
})

/* 监听连接 */
client.on("connect", () => {
  console.log("连接成功")
  /* 订阅主题 */
  client.subscribe("test:topic")
})

/* 接受消息 */
client.on("message", (topic, msg) => {
  console.log(topic, msg.toString())
})

你可能感兴趣的:(vue.js,javascript,前端)