indexbackoff.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="content">
  3. <!-- <image class="logo" src="/static/logo.png"></image> -->
  4. <text class="title">发布消息</text>
  5. <view class="br"></view>
  6. <view class="text-area">
  7. <text>Topic</text>
  8. <input class="edittext" v-model="publishTopic" />
  9. </view>
  10. <view class="br"></view>
  11. <view class="text-area">
  12. <text>Payload</text>
  13. <textarea class="edittext" v-model="publishMsg" />
  14. </view>
  15. <view class="br"></view>
  16. <button @click="publish">publish</button>
  17. <text class="title">订阅主题</text>
  18. <view class="text-area">
  19. <!-- 订阅 -->
  20. <input class="edittext" v-model="subscribeTopic" />
  21. </view>
  22. <view class="br"></view>
  23. <button @click="subscribe">subscribe</button>
  24. <view class="br"></view>
  25. <text class="title">收到的Message</text>
  26. <view class="br"></view>
  27. <view v-for="(item, index) in subList" :key="index">
  28. <text>Topic:{{ item.topic }} ,Payload: {{ item.msg }}</text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import mqtt from 'mqtt/dist/mqtt.js'
  34. //let mqtt = require('mqtt/dist/mqtt.js')
  35. const options = {
  36. connectTimeout: 4000, // 超时时间
  37. clientId: "mqtt_" + parseInt(Math.random() * 100 + 800, 10),
  38. port: 8081,
  39. };
  40. // #ifdef H5
  41. let url = 'wss://test.mosquitto.org'
  42. // #endif
  43. // #ifdef MP-WEIXIN||APP-PLUS
  44. let url = 'wxs://test.mosquitto.org'
  45. // #endif
  46. let client = mqtt.connect(url, options)
  47. export default {
  48. data() {
  49. return {
  50. publishMsg: "",
  51. publishTopic: "",
  52. subscribeTopic: "",
  53. subList: [],
  54. isConnect: false,
  55. }
  56. },
  57. onLoad() {
  58. //this.init();
  59. },
  60. created() {
  61. this.init();
  62. },
  63. methods: {
  64. publish() {
  65. //发布消息
  66. if (this.isConnect) {
  67. client.publish(this.publishTopic, this.publishMsg, (err) => {
  68. if (err) {
  69. //alert(`发布消息失败`);
  70. uni.showToast({
  71. title: "发布消息失败",
  72. duration: 2000,
  73. icon: "none"
  74. })
  75. }
  76. });
  77. }
  78. },
  79. subscribe() {
  80. if (this.isConnect) {
  81. client.subscribe(this.subscribeTopic, (err) => {
  82. if (!err) {
  83. uni.showToast({
  84. title: `订阅${this.subscribeTopic}主题成功`,
  85. duration: 2000,
  86. icon: "none"
  87. })
  88. } else {
  89. uni.showToast({
  90. title: `订阅${this.subscribeTopic}主题失败`,
  91. duration: 2000,
  92. icon: "none"
  93. })
  94. }
  95. });
  96. }
  97. },
  98. init() {
  99. const that = this;
  100. //连接成功回调
  101. client.on("connect", function() {
  102. that.isConnect = true
  103. console.log("connect success!");
  104. });
  105. //异常回调
  106. client.on("error", function(err) {
  107. that.isConnect = false
  108. console.log(err);
  109. });
  110. client.on("offline", function() {
  111. that.isConnect = false
  112. console.log("offline");
  113. });
  114. client.on("close", function() {
  115. that.isConnect = false
  116. console.log("close");
  117. });
  118. //收到消息的回调
  119. client.on("message", function(topic, message) {
  120. console.log(`message = ${message.toString()}`);
  121. that.subList.push({
  122. topic: topic.toString(),
  123. msg: message.toString(),
  124. });
  125. });
  126. },
  127. }
  128. }
  129. </script>
  130. <style>
  131. .content {
  132. display: flex;
  133. flex-direction: column;
  134. /* align-items: center; */
  135. justify-content: center;
  136. margin: 20rpx;
  137. }
  138. .logo {
  139. height: 200rpx;
  140. width: 200rpx;
  141. margin-top: 200rpx;
  142. margin-left: auto;
  143. margin-right: auto;
  144. margin-bottom: 50rpx;
  145. }
  146. .br {
  147. height: 10rpx;
  148. }
  149. .text-area {
  150. display: flex;
  151. /* justify-content: center; */
  152. }
  153. .title {
  154. font-size: 20rpx;
  155. color: #2c3e50;
  156. }
  157. textarea {
  158. height: 50rpx;
  159. }
  160. .edittext {
  161. display: flex;
  162. border: 1rpx solid #333333;
  163. }
  164. </style>