Fuyuu
1 year ago
5 changed files with 170 additions and 10 deletions
After Width: | Height: | Size: 1.8 KiB |
@ -0,0 +1,138 @@ |
|||
// import * as mqtt from 'mqtt';
|
|||
// import $mitt from "../mitt/mitt";
|
|||
import $mitt from '@/utils/earthMap/mitt'; |
|||
// export default {
|
|||
// data() {
|
|||
// return {
|
|||
// connection: {
|
|||
// host: '192.168.1.101',
|
|||
// port: 8083,
|
|||
// connectTimeout: 4000, // 超时时间
|
|||
// reconnectPeriod: 4000, // 重连时间间隔
|
|||
// username: 'admin',
|
|||
// password: 'public',
|
|||
// },
|
|||
// client: {
|
|||
// connected: false,
|
|||
// },
|
|||
// // 订阅的频道和事件
|
|||
// topicList: {
|
|||
// "ship/gps/100": "mqttShipShow",
|
|||
// "/v1.0.0/+/up/event": "智能围网",
|
|||
// "/server/cmd/+/zdgl":"设备:[消音/复位/重启]"
|
|||
// }
|
|||
// }
|
|||
// },
|
|||
// created() {
|
|||
// this.createConnection()
|
|||
// },
|
|||
// methods: {
|
|||
// // 创建连接
|
|||
// createConnection() {
|
|||
// const { host, port, endpoint, ...options } = this.connection
|
|||
// const connectUrl = `mqtt://${host}:${port}/mqtt`
|
|||
// try {
|
|||
// this.client = mqtt.connect(connectUrl, options)
|
|||
|
|||
// } catch (error) {
|
|||
// console.log('mqtt.connect error', error)
|
|||
// }
|
|||
// this.client.on('connect', () => {
|
|||
// console.log('Connection succeeded!')
|
|||
// // 不会订阅多个频道,所以遍历对象,循环监听多个频道
|
|||
// for (let key in this.topicList) {
|
|||
// this.client.subscribe(key, (err) => {
|
|||
// if (!err) {
|
|||
// console.log(`订阅'${key}'成功`);
|
|||
// }
|
|||
// });
|
|||
// }
|
|||
// })
|
|||
// this.client.on('error', error => {
|
|||
// console.log('Connection failed', error)
|
|||
// })
|
|||
// /**
|
|||
// * topic: 监听的频道
|
|||
// * message: 返回的数据
|
|||
// */
|
|||
// this.client.on('message', (topic, message) => {
|
|||
// this[this.topicList[topic]](message)
|
|||
// })
|
|||
// },
|
|||
// mqttShipShow(message) {
|
|||
// const row = JSON.parse(message)
|
|||
// // 处理逻辑
|
|||
// fn(row)
|
|||
// }
|
|||
// },
|
|||
// }
|
|||
console.log(mqtt); |
|||
// const host = import.meta.env.VUE_APP_MQTT_HOST || '127.0.0.1';
|
|||
const host = '192.168.1.200'; |
|||
const port = import.meta.env.VUE_APP_MQTT_PORT || 8083; |
|||
const options = { |
|||
//mqtt配置
|
|||
// port: 8083,//连接端口
|
|||
protocolId: 'MQTT', |
|||
connectTimeout: 4000, //超时时间
|
|||
clientID: 'mqtt_' + Math.random().toString(16).substr(2, 8), |
|||
username: 'admin', |
|||
password: 'public', |
|||
}; |
|||
// const options = {
|
|||
// clean: true,
|
|||
// connectTimeout: 4000, //超时时间
|
|||
// reconnectPeriod: 1000, //重连时间间隔
|
|||
// clientId: 'emqx_test',
|
|||
// username: 'emqx_test',
|
|||
// password: 'emqx_test',
|
|||
// };
|
|||
|
|||
// 订阅的频道和事件
|
|||
const topics = '/server/radar'; |
|||
const topicList = topics.split(','); |
|||
// const topicList = {
|
|||
// "ship/gps/100": "mqttShipShow",
|
|||
// //server=服务 warning=预警 +代表所有级别预警 1,2,3 zdgl=震动光缆
|
|||
// "server/warning/+/zdgl": "系统发布智能围网预警",
|
|||
// "/v1.0.0/+/up/event": "智能围网",
|
|||
// "/server/cmd/+/zdgl":"设备[消音复位重启]"
|
|||
// };
|
|||
// $mitt.on("warnInfo", (msg) => {console.log(msg) });
|
|||
export default class MqttUnit { |
|||
connectUrl = `mqtt://${host}:${port}/mqtt`; |
|||
// connectUrl = `ws://broker.emqx.io:8083/mqtt`;
|
|||
|
|||
client = mqtt.connect(this.connectUrl, options); |
|||
|
|||
mqttInit() { |
|||
const connectUrl = `mqtt://${host}:${port}/mqtt`; |
|||
// const connectUrl = `ws://broker.emqx.io:8083/mqtt`;
|
|||
|
|||
this.client.on('connect', () => { |
|||
console.log(`MQTT Connection ${connectUrl} succeeded!`); |
|||
// 不会订阅多个频道,所以遍历对象,循环监听多个频道
|
|||
for (let key of topicList) { |
|||
this.client.subscribe(key, (err) => { |
|||
if (!err) { |
|||
console.log(`订阅'${key}'成功`); |
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
this.client.on('error', (error) => { |
|||
console.log('Connection failed', error); |
|||
}); |
|||
/** |
|||
* topic: 监听的频道 |
|||
* message: 返回的数据 |
|||
*/ |
|||
this.client.on('message', (topic, message) => { |
|||
const utf8decoder = new TextDecoder(); |
|||
const dataString = utf8decoder.decode(message); |
|||
if (topic.startsWith('/server/')) { |
|||
$mitt.emit('deviceCmd', dataString); |
|||
} |
|||
}); |
|||
} |
|||
} |
Loading…
Reference in new issue