123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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")
- }
- })
- }
-
- }
-
- }
|