import?QRCode?from?'qrcodejs2'?//?引入qrcode
import?{?v1?as?uuidv1?}?from?'uuid'
import?PahoMQTT?from?'paho-mqtt'
??mounted()?{???
? ? this.clientId?=?uuidv1()????
????let?codeUrl?=?'掃碼要跳轉的地址‘??
????this.qrcode(codeUrl)????
????this.CreateMQTT()
}
?methods:?{
????qrcode(url)?{?????
?????????let?qrcode?=?new?QRCode('qrcode',?{????????
? ? ? ? ?text:?url,?//?二維碼地址
? ? ? ? ?width: 230,
? ? ? ? ?height: 230,
? ? ? ? ?colorDark:?'#000000', colorLight:?'#ffffff',? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????????correctLevel:?QRCode.CorrectLevel.H,??????????????????????????????????????????????????????????????????????????????})????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????},?
???CreateMQTT()?{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?let?rand?=?Math.ceil(Math.random()?*?10000)?+?''? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
?????????this.topic?=?'/******/'?+?this.clientId?????????
?????????client?=?new?PahoMQTT.Client('xxx.cn',?Number(8084),?this.topic)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?client.onConnectionLost?=?this.onConnectionLost?
????????//注冊連接斷開處理事件? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?????????client.onMessageArrived?=?this.onMessageArrived?
????????//注冊消息接收處理事件? ? ? ?????????client.connect({?onSuccess:?this.onConnect,?useSSL:?true?})?????????????????????? ? ? ? ? ? ? ? },
???onConnect()?{??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????client.subscribe(this.topic,?0)?//訂閱主題? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?console.log('onConnected')? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????},
???onConnectionLost:?function?(responseObject)?{??????????????????????????????????????????????????????????????????????????if?(responseObject.errorCode?!==?0)?{????????????????????????????????????????????????????????????????????????????????????????console.log('onConnectionLost:'?+?responseObject.errorMessage)? ? ? ????????????setTimeout(function?()?{??????????????????????????????????????????????????????????????????????????????????????????????????????????????console.log('重連')? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????client.connect({?onSuccess:?this.onConnect,?useSSL:?true?})? ? ? ? ? ? ? ? ? ? ? ? ?????????????},?10000) }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
??onMessageArrived(message)?{??????????????????????????????????????????????????????????????????????????????????????????????????????console.log('onMessageArrived:'?+?message.payloadString)??????????????????????????????????????????????if?(message.payloadString?==?'close')?return?????????????????????????????????????????????????????????????????????//接收到數(shù)據(jù)后可以進行后續(xù)操作...? },
}