feat: 增加自动重连功能
This commit is contained in:
@@ -110,6 +110,9 @@ class MqttManager {
|
|||||||
password: '',
|
password: '',
|
||||||
keepAlive: 60,
|
keepAlive: 60,
|
||||||
cleanSession: true,
|
cleanSession: true,
|
||||||
|
connectTimeout: 10000,
|
||||||
|
reconnect: true,
|
||||||
|
reconnectInterval: 5000,
|
||||||
status: 'disconnected',
|
status: 'disconnected',
|
||||||
topics: [
|
topics: [
|
||||||
{ id: 'topic_default_001', topic: 'sensor/+/temperature', qos: 1, subscribed: false },
|
{ id: 'topic_default_001', topic: 'sensor/+/temperature', qos: 1, subscribed: false },
|
||||||
@@ -175,6 +178,9 @@ class MqttManager {
|
|||||||
password: config.password || '',
|
password: config.password || '',
|
||||||
keepAlive: config.keepAlive || 60,
|
keepAlive: config.keepAlive || 60,
|
||||||
cleanSession: config.cleanSession !== false,
|
cleanSession: config.cleanSession !== false,
|
||||||
|
connectTimeout: config.connectTimeout ?? 10000,
|
||||||
|
reconnect: config.reconnect !== false,
|
||||||
|
reconnectInterval: config.reconnectInterval ?? 5000,
|
||||||
status: 'disconnected',
|
status: 'disconnected',
|
||||||
topics: [],
|
topics: [],
|
||||||
};
|
};
|
||||||
@@ -205,6 +211,9 @@ class MqttManager {
|
|||||||
username: config.username || '',
|
username: config.username || '',
|
||||||
keepAlive: config.keepAlive || 60,
|
keepAlive: config.keepAlive || 60,
|
||||||
cleanSession: config.cleanSession !== false,
|
cleanSession: config.cleanSession !== false,
|
||||||
|
connectTimeout: config.connectTimeout ?? server.connectTimeout ?? 10000,
|
||||||
|
reconnect: config.reconnect !== false,
|
||||||
|
reconnectInterval: config.reconnectInterval ?? server.reconnectInterval ?? 5000,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (config.password) {
|
if (config.password) {
|
||||||
@@ -254,8 +263,8 @@ class MqttManager {
|
|||||||
password: server.password || undefined,
|
password: server.password || undefined,
|
||||||
keepalive: server.keepAlive,
|
keepalive: server.keepAlive,
|
||||||
clean: server.cleanSession,
|
clean: server.cleanSession,
|
||||||
reconnectPeriod: 5000,
|
reconnectPeriod: 0,
|
||||||
connectTimeout: 10000,
|
connectTimeout: server.connectTimeout,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -265,6 +274,8 @@ class MqttManager {
|
|||||||
client.on('connect', () => {
|
client.on('connect', () => {
|
||||||
server.status = 'connected';
|
server.status = 'connected';
|
||||||
this.notify('status-change', { id, status: 'connected' });
|
this.notify('status-change', { id, status: 'connected' });
|
||||||
|
// 只在首次连接成功后,再根据配置启用自动重连
|
||||||
|
client.options.reconnectPeriod = server.reconnect ? server.reconnectInterval : 0;
|
||||||
// 自动订阅已有主题
|
// 自动订阅已有主题
|
||||||
server.topics.forEach((t) => {
|
server.topics.forEach((t) => {
|
||||||
this._doSubscribe(id, t.topic, { qos: t.qos });
|
this._doSubscribe(id, t.topic, { qos: t.qos });
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch } from 'vue';
|
import { ref, watch } from 'vue';
|
||||||
import { NModal, NForm, NFormItem, NGrid, NFormItemGi, NInput, NInputNumber, NSelect, NSwitch, NButton, useMessage } from 'naive-ui';
|
import { NModal, NForm, NGrid, NFormItemGi, NInput, NInputNumber, NSelect, NSwitch, NButton, useMessage } from 'naive-ui';
|
||||||
import { useMqttStore } from '../stores/mqtt.js';
|
import { useMqttStore } from '../stores/mqtt.js';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
@@ -24,6 +24,9 @@ const form = ref({
|
|||||||
password: '',
|
password: '',
|
||||||
keepAlive: 60,
|
keepAlive: 60,
|
||||||
cleanSession: true,
|
cleanSession: true,
|
||||||
|
connectTimeout: 10000,
|
||||||
|
reconnect: true,
|
||||||
|
reconnectInterval: 5000,
|
||||||
});
|
});
|
||||||
|
|
||||||
const rules = {
|
const rules = {
|
||||||
@@ -57,6 +60,9 @@ watch(
|
|||||||
password: '',
|
password: '',
|
||||||
keepAlive: props.editServer.keepAlive || 60,
|
keepAlive: props.editServer.keepAlive || 60,
|
||||||
cleanSession: props.editServer.cleanSession !== false,
|
cleanSession: props.editServer.cleanSession !== false,
|
||||||
|
connectTimeout: props.editServer.connectTimeout ?? 10000,
|
||||||
|
reconnect: props.editServer.reconnect !== false,
|
||||||
|
reconnectInterval: props.editServer.reconnectInterval ?? 5000,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
isEdit.value = false;
|
isEdit.value = false;
|
||||||
@@ -70,6 +76,9 @@ watch(
|
|||||||
password: '',
|
password: '',
|
||||||
keepAlive: 60,
|
keepAlive: 60,
|
||||||
cleanSession: true,
|
cleanSession: true,
|
||||||
|
connectTimeout: 10000,
|
||||||
|
reconnect: true,
|
||||||
|
reconnectInterval: 5000,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,9 +196,42 @@ async function handleSave() {
|
|||||||
/>
|
/>
|
||||||
</n-form-item-gi>
|
</n-form-item-gi>
|
||||||
</n-grid>
|
</n-grid>
|
||||||
<n-form-item label="Clean Session">
|
<n-grid
|
||||||
<n-switch v-model:value="form.cleanSession" />
|
:cols="2"
|
||||||
</n-form-item>
|
:x-gap="12"
|
||||||
|
>
|
||||||
|
<n-form-item-gi label="Clean Session">
|
||||||
|
<n-switch v-model:value="form.cleanSession" />
|
||||||
|
</n-form-item-gi>
|
||||||
|
<n-form-item-gi label="自动重连">
|
||||||
|
<n-switch v-model:value="form.reconnect" />
|
||||||
|
</n-form-item-gi>
|
||||||
|
<n-form-item-gi
|
||||||
|
label="连接超时 (毫秒)"
|
||||||
|
path="connectTimeout"
|
||||||
|
>
|
||||||
|
<n-input-number
|
||||||
|
v-model:value="form.connectTimeout"
|
||||||
|
:min="1000"
|
||||||
|
:max="120000"
|
||||||
|
:step="1000"
|
||||||
|
style="width: 100%"
|
||||||
|
/>
|
||||||
|
</n-form-item-gi>
|
||||||
|
<n-form-item-gi
|
||||||
|
label="重连间隔 (毫秒)"
|
||||||
|
path="reconnectInterval"
|
||||||
|
>
|
||||||
|
<n-input-number
|
||||||
|
v-model:value="form.reconnectInterval"
|
||||||
|
:min="1000"
|
||||||
|
:max="300000"
|
||||||
|
:step="1000"
|
||||||
|
:disabled="!form.reconnect"
|
||||||
|
style="width: 100%"
|
||||||
|
/>
|
||||||
|
</n-form-item-gi>
|
||||||
|
</n-grid>
|
||||||
</n-form>
|
</n-form>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<div style="display: flex; justify-content: flex-end; gap: 8px">
|
<div style="display: flex; justify-content: flex-end; gap: 8px">
|
||||||
|
|||||||
Reference in New Issue
Block a user