moduleMqtt1.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import mqtt from '../../common/mqtt.js'
  2. //import mqtt from 'mqtt/dist/mqtt.js'
  3. const options = {
  4. clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
  5. keepalive: 28,//每28秒发送一次心跳
  6. connectTimeout: 15 * 1000,//连接超时的时间
  7. };
  8. let url = 'wss://mqtt.test.radio1964.com'
  9. // #ifdef MP-WEIXIN
  10. url = 'wxs://mqtt.test.radio1964.com'
  11. // #endif
  12. export default {
  13. namespaced: true,
  14. state:{
  15. //用户设备列表
  16. deviceList:[],
  17. //mqtt客户端
  18. mqttClient: undefined,
  19. //收到的消息的历史记录
  20. msgList:[],
  21. },
  22. getters:{
  23. //mqtt连接状态
  24. connected:state =>{
  25. return state.mqttClient !== undefined && state.mqttClient.connected
  26. },
  27. },
  28. mutations:{
  29. //添加设备
  30. addDevice(state,device){
  31. let findDevice = state.deviceList.find((item) =>{
  32. return item.ssid === device.ssid
  33. })
  34. if(typeof findDevice === 'undefined'){
  35. state.deviceList.push(device)
  36. }
  37. //连接服务器
  38. this.commit('connect',options.clientId)
  39. },
  40. //连接mqtt服务器 clientId为wx_加上用户的uid
  41. connect(state,clientId) {
  42. function connect(state,clientId){
  43. console.log('connect')
  44. options.clientId = clientId
  45. state.mqttClient = mqtt.connect(url, options)
  46. //连接成功回调
  47. state.mqttClient.on("connect", function() {
  48. console.log("connect success!");
  49. });
  50. //异常回调
  51. state.mqttClient.on("error", function(err) {
  52. console.log(err);
  53. });
  54. state.mqttClient.on('offline', function() {
  55. console.log(`offline callback`);
  56. })
  57. state.mqttClient.on('end', function() {
  58. console.log(`end callback`);
  59. })
  60. //网络断开的回调
  61. state.mqttClient.on('close', function() {
  62. console.log(`close callback`);
  63. })
  64. //收到消息的回调
  65. state.mqttClient.on('message',function(topic,message){
  66. console.log(`message = ${message.toString()}`);
  67. // state.msgList.push({
  68. // topic: topic.toString(),
  69. // msg: message.toString(),
  70. // });
  71. })
  72. }
  73. if (typeof state.mqttClient === 'undefined') {
  74. connect(state,clientId)
  75. } else {
  76. if(options.clientId !==clientId){
  77. //断开连接后,重新连接,因为clientId不同
  78. state.mqttClient.end(false,() =>{
  79. state.mqttClient = undefined
  80. connect(state,clientId)
  81. })
  82. }
  83. if (!state.mqttClient.connected) {
  84. console.log('reconnect')
  85. state.mqttClient.reconnect()
  86. //重连成功的回调
  87. state.mqttClient.on("reconnect", function() {
  88. console.log("reconnect success!");
  89. });
  90. }
  91. }
  92. },
  93. disconnect(state) {
  94. console.log('disconnect')
  95. if (state.mqttClient !== undefined) {
  96. //mqtt.js在小程序运行有问题,调用end 不会回调close,而且state.mqttClient.connected一直是true
  97. state.mqttClient.end(false,() =>{
  98. console.log(`state.mqttClient.connected = ${state.mqttClient.connected}`);
  99. })
  100. //state.mqttClient = undefined
  101. }
  102. },
  103. //token失效,退出登录,需要断开连接
  104. release(state) {
  105. if (state.mqttClient !== undefined) {
  106. state.mqttClient.end()
  107. state.mqttClient = undefined
  108. }
  109. }
  110. },
  111. actions:{
  112. //发布消息
  113. publish({state,commit,getters},message){
  114. console.log('publish');
  115. console.log(message);
  116. return new Promise((resolve,reject) =>{
  117. if(getters.connected){
  118. //发布消息
  119. state.mqttClient.publish(message.topic,message.payload,(err) =>{
  120. if(err){
  121. console.log(err);
  122. reject(err)
  123. }else{
  124. resolve()
  125. }
  126. })
  127. }else{
  128. reject("mqttClient is not connected")
  129. }
  130. })
  131. },
  132. //订阅消息
  133. subscribe({state,commit,getters},topic){
  134. console.log(`subscribe topic = ${topic}`);
  135. return new Promise((resolve,reject) =>{
  136. if(getters.connected){
  137. //发布消息
  138. state.mqttClient.subscribe(topic,(err) =>{
  139. if(err){
  140. console.log(err);
  141. reject(err)
  142. }else{
  143. resolve()
  144. }
  145. })
  146. }else{
  147. reject("mqttClient is not connected")
  148. }
  149. })
  150. }
  151. }
  152. }