<template>
<view class="content">
<button @click="connect" type="primary">connect</button>
<view class="log">
<view v-for="(log,index) in logs" class="" :key="index">
{{log}}
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
logs: []
}
},
methods: {
connect() {
var self = this
self.logs.push('connect')
var mqtt = require('mqtt/dist/mqtt.js')
// #ifdef H5
var client = mqtt.connect('ws://myserver:8083/mqtt')
// #endif
// #ifdef MP-WEIXIN||APP-PLUS
var client = mqtt.connect('wx://myserver:8083/mqtt')
// #endif
client.on('connect', function() {
self.logs.push('on connect')
client.subscribe('presence', function(err) {
if (!err) {
client.publish('presence', 'hello mqtt')
}
})
}).on('reconnect', function() {
self.logs.push('on reconnect')
}).on('error', function() {
self.logs.push('on error')
}).on('end', function() {
self.logs.push('on end')
}).on('message', function(topic, message) {
console.log(message)
self.logs.push(message.toString())
})
}
}
}
</script>
<style>
.content {
text-align: center;
height: 400upx;
word-break: break-all;
}
</style>
0 个回复