Mqtt链接:
initMqtt() {if (!this.client) {this.client = null;}const connectUrl = "ws://地址/mqtt";const clientId = `app_${Math.random().toString(16).slice(3)}`;const mqtt = require("../../../../node_modules/mqtt/dist/mqtt.min");const options = {clean: true,connectTimeout: 4000,clientId,username: "admin",password: "密码",};this.client = mqtt.connect(connectUrl, options);this.client.on("connect", () => {console.log("连接成功....");this.mqttReceive();});this.client.on("error", (err) => {console.log("err=>", err);this.client.end();});},mqttReceive() {const topic = `/device/wavve/vital/sign/${this.deviceSn}`;this.client.subscribe(topic, (err) => {if (!err) {console.log("subscribe success!已发送主题", topic);} else {console.log("err", err);}});this.client.on("message", (topic, message) => {// console.log(message, "接收的报文");const data = JSON.parse(message.toString());const parsedData = JSON.parse(data);const body = parsedData.heartBpm;// console.log(body, "parsedData");this.heartRate = parsedData.heartBpm; // 更新 heartRate 值this.callheartRate = parsedData.breathBpm; // 更新 heartRate 值this.state = parsedData.state;});},//关闭mqttdisconnectMqtt() {if (this.client) {this.client.end();console.log("MQTT 关闭");}},