54 lines
868 B
Vue
54 lines
868 B
Vue
![]() |
<template>
|
||
|
<view class="container">
|
||
|
<view class="section">
|
||
|
<text>MQTT 连接测试</text>
|
||
|
</view>
|
||
|
</view>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
|
import mqtt from 'mqtt'
|
||
|
|
||
|
export default {
|
||
|
data() {
|
||
|
return {
|
||
|
client: null, // mqtt连接 对象
|
||
|
host: 'wxs://szdot.top', // mqtt服务器地址
|
||
|
options: { // mqtt服务器 参数配置
|
||
|
port: 8083,
|
||
|
connectTimeout: 3000,
|
||
|
clientId: 'uni_cl',
|
||
|
clean: true,
|
||
|
keepalive: 60,
|
||
|
username: 'admin',
|
||
|
password: '123456'
|
||
|
},
|
||
|
}
|
||
|
},
|
||
|
onLoad() {
|
||
|
try{
|
||
|
this.client = mqtt.connect(this.host, this.options)
|
||
|
setTimeout(()=> {
|
||
|
this.client.publish('demo','hello world', {
|
||
|
qos: 2
|
||
|
})
|
||
|
}, 3000);
|
||
|
}catch(e){
|
||
|
console.log(e)
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
|
||
|
},
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<style>
|
||
|
.container {
|
||
|
padding: 20px;
|
||
|
}
|
||
|
|
||
|
.section {
|
||
|
margin-bottom: 20px;
|
||
|
}
|
||
|
</style>
|