徐景良 2025-12-31 09:11:32 +08:00
commit 63c0f3b819
1 changed files with 20 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import { timestampToFormattedDate } from '@/utils/index';
const userStore = useUserStore();
const userInfo = userStore.getUserInfo;
let connectLock = false
const { createMessage } = useMessage();
const seizeClientId = 'mqtt_client_1581F8HGX254V00A0BUY_seize';
@ -55,6 +56,21 @@ const createConnection = (callback?) => {
// 连接mqtt的时候先用GetRedisUser查一下有没有
// 然后AddOrUpdateRedisUser更新或添加
// 控制的时候先查一下有没有锁定的用户GetLockedClients
if(connectLock){
if (callback) {
if(client && client.connected){
callback();
}else{
setTimeout(() => {
client.on('connect', () => {
callback();
});
}, 3000)
}
}
return
}
connectLock = true
try {
getRedisUser(userInfo.id).then((res) => {
console.log("getRedisUser",res);
@ -84,9 +100,9 @@ const createConnection = (callback?) => {
// 重连回调
client.on('reconnect', () => {
console.log('MQTT正在重连...');
if(!client || !client.connected){
client = mqtt.connect(connectUrl, options);
}
// if(!client || !client.connected){
// client = mqtt.connect(connectUrl, options);
// }
});
// 接收到消息
client.on("messasge",()=>{
@ -120,7 +136,7 @@ const destroyConnection = () => {
};
// 订阅事件
const clientSubscribe = (topic: string, options?: any) => {
if(!topic){
if(topic.includes('/null/')){
return
}
// if (!client || !client.connected) {