import mqtt from '../../common/mqtt.js' //import mqtt from 'mqtt/dist/mqtt.js' const options = { clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10), keepalive: 28,//每28秒发送一次心跳 connectTimeout: 15 * 1000,//连接超时的时间 }; let url = 'wss://mqtt.test.radio1964.com' // #ifdef MP-WEIXIN url = 'wxs://mqtt.test.radio1964.com' // #endif export default { namespaced: true, state:{ //用户设备列表 deviceList:[], //mqtt客户端 mqttClient: undefined, //收到的消息的历史记录 msgList:[], }, getters:{ //mqtt连接状态 connected:state =>{ return state.mqttClient !== undefined && state.mqttClient.connected }, }, mutations:{ //添加设备 addDevice(state,device){ let findDevice = state.deviceList.find((item) =>{ return item.ssid === device.ssid }) if(typeof findDevice === 'undefined'){ state.deviceList.push(device) } //连接服务器 this.commit('connect',options.clientId) }, //连接mqtt服务器 clientId为wx_加上用户的uid connect(state,clientId) { function connect(state,clientId){ console.log('connect') options.clientId = clientId state.mqttClient = mqtt.connect(url, options) //连接成功回调 state.mqttClient.on("connect", function() { console.log("connect success!"); }); //异常回调 state.mqttClient.on("error", function(err) { console.log(err); }); state.mqttClient.on('offline', function() { console.log(`offline callback`); }) state.mqttClient.on('end', function() { console.log(`end callback`); }) //网络断开的回调 state.mqttClient.on('close', function() { console.log(`close callback`); }) //收到消息的回调 state.mqttClient.on('message',function(topic,message){ console.log(`message = ${message.toString()}`); // state.msgList.push({ // topic: topic.toString(), // msg: message.toString(), // }); }) } if (typeof state.mqttClient === 'undefined') { connect(state,clientId) } else { if(options.clientId !==clientId){ //断开连接后,重新连接,因为clientId不同 state.mqttClient.end(false,() =>{ state.mqttClient = undefined connect(state,clientId) }) } if (!state.mqttClient.connected) { console.log('reconnect') state.mqttClient.reconnect() //重连成功的回调 state.mqttClient.on("reconnect", function() { console.log("reconnect success!"); }); } } }, disconnect(state) { console.log('disconnect') if (state.mqttClient !== undefined) { //mqtt.js在小程序运行有问题,调用end 不会回调close,而且state.mqttClient.connected一直是true state.mqttClient.end(false,() =>{ console.log(`state.mqttClient.connected = ${state.mqttClient.connected}`); }) //state.mqttClient = undefined } }, //token失效,退出登录,需要断开连接 release(state) { if (state.mqttClient !== undefined) { state.mqttClient.end() state.mqttClient = undefined } } }, actions:{ //发布消息 publish({state,commit,getters},message){ console.log('publish'); console.log(message); return new Promise((resolve,reject) =>{ if(getters.connected){ //发布消息 state.mqttClient.publish(message.topic,message.payload,(err) =>{ if(err){ console.log(err); reject(err) }else{ resolve() } }) }else{ reject("mqttClient is not connected") } }) }, //订阅消息 subscribe({state,commit,getters},topic){ console.log(`subscribe topic = ${topic}`); return new Promise((resolve,reject) =>{ if(getters.connected){ //发布消息 state.mqttClient.subscribe(topic,(err) =>{ if(err){ console.log(err); reject(err) }else{ resolve() } }) }else{ reject("mqttClient is not connected") } }) } } }