index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view class="content">
  3. <view>
  4. <button type="primary" @click="connectOrDisconnect">{{connectText}}</button>
  5. </view>
  6. <!-- <image class="logo" src="/static/logo.png"></image> -->
  7. <text class="title">发布消息</text>
  8. <view class="br"></view>
  9. <view class="text-area">
  10. <text>Topic</text>
  11. <input class="edittext" v-model="publishTopic" />
  12. </view>
  13. <view class="br"></view>
  14. <view class="text-area">
  15. <text>Payload</text>
  16. <textarea class="edittext" v-model="publishMsg" />
  17. </view>
  18. <view class="br"></view>
  19. <button @click="publish">publish</button>
  20. <text class="title">订阅主题</text>
  21. <view class="text-area">
  22. <!-- 订阅 -->
  23. <input class="edittext" v-model="subscribeTopic" />
  24. </view>
  25. <view class="br"></view>
  26. <button @click="subscribe">subscribe</button>
  27. <view class="br"></view>
  28. <text class="title">收到的Message</text>
  29. <view class="br"></view>
  30. <view v-for="(item, index) in subList" :key="index">
  31. <text>Topic:{{ item.topic }} ,Payload: {{ item.msg }}</text>
  32. </view>
  33. <view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. //import store from '@/store/index.js';//需要引入store
  39. export default {
  40. data() {
  41. return {
  42. publishMsg: "",
  43. publishTopic: "",
  44. subscribeTopic: "",
  45. }
  46. },
  47. onLoad() {
  48. console.log('index.vue onload')
  49. },
  50. created() {
  51. console.log('index.vue created')
  52. },
  53. computed: {
  54. connectText() {
  55. return (this.$store.getters['moduleMqtt/connected']) ? '断开' : '连接'
  56. },
  57. subList() {
  58. return this.$store.state.moduleMqtt.msgList
  59. },
  60. },
  61. methods: {
  62. publish() { //发布
  63. let that = this;
  64. //发布消息
  65. that.$store.dispatch({
  66. type: 'moduleMqtt/publish',
  67. topic: that.publishTopic,
  68. payload: that.publishMsg,
  69. }).then(() => {/* success */}, (err) =>{
  70. console.log(err);
  71. uni.showToast({
  72. title: "发布消息失败",
  73. duration: 2000,
  74. icon: "none"
  75. });
  76. });
  77. },
  78. subscribe() { //订阅
  79. let that = this;
  80. //订阅消息
  81. that.$store.dispatch('moduleMqtt/subscribe', that.subscribeTopic)
  82. .then(() => {
  83. uni.showToast({
  84. title: `订阅${that.subscribeTopic}主题成功`,
  85. duration: 2000,
  86. icon: "none"
  87. })
  88. },
  89. (err) => {
  90. uni.showToast({
  91. title: `订阅${that.subscribeTopic}主题失败`,
  92. duration: 2000,
  93. icon: "none"
  94. })
  95. });
  96. },
  97. connectOrDisconnect() { //连接或者断开mqtt服务
  98. if (this.$store.getters['moduleMqtt/connected'] === false) {
  99. //连接mqtt服务器
  100. this.$store.commit('moduleMqtt/connect')
  101. } else {
  102. //断开连接
  103. this.$store.commit('moduleMqtt/disconnect')
  104. }
  105. },
  106. connectWithWxApi(){
  107. uni.connectSocket({
  108. url:'wss://mqtt.test.radio1964.com',
  109. success(res){
  110. console.log('连接成功');
  111. },
  112. fail(err){
  113. console.log('连接失败');
  114. }
  115. });
  116. uni.onSocketOpen(function(res){
  117. console.log('onSocketOpen');
  118. console.log(res);
  119. })
  120. uni.onSocketClose(function(res){
  121. console.log('onSocketClose');
  122. console.log(res);
  123. })
  124. uni.onSocketError(function(err){
  125. console.log('err');
  126. console.log(err);
  127. })
  128. },
  129. closeSocket(){
  130. uni.closeSocket({
  131. success() {
  132. console.log("closeSocket success");
  133. },
  134. fail() {
  135. console.log("closeSocket fail");
  136. }
  137. })
  138. },
  139. godoBle() { //添加设备
  140. // #ifdef MP-WEIXIN||APP-PLUS
  141. uni.navigateTo({
  142. url: '../ble/ScanBleDevice'
  143. })
  144. // #endif
  145. // #ifdef H5
  146. uni.navigateTo({
  147. url: '../ble/ConnectBleDevice'
  148. })
  149. // uni.showToast({
  150. // title:'H5页面不支持扫描设备',
  151. // icon:'none'
  152. // })
  153. // #endif
  154. }
  155. }
  156. }
  157. </script>
  158. <style>
  159. .content {
  160. display: flex;
  161. flex-direction: column;
  162. /* align-items: center; */
  163. justify-content: center;
  164. margin: 20rpx;
  165. }
  166. .logo {
  167. height: 200rpx;
  168. width: 200rpx;
  169. margin-top: 200rpx;
  170. margin-left: auto;
  171. margin-right: auto;
  172. margin-bottom: 50rpx;
  173. }
  174. .br {
  175. height: 10rpx;
  176. }
  177. .text-area {
  178. display: flex;
  179. /* justify-content: center; */
  180. }
  181. .title {
  182. font-size: 30rpx;
  183. color: #2c3e50;
  184. margin: 5rpx;
  185. }
  186. textarea {
  187. height: 50rpx;
  188. }
  189. .edittext {
  190. display: flex;
  191. border: 1rpx solid #333333;
  192. }
  193. </style>