This commit is contained in:
Iruka 2024-09-30 11:01:45 +08:00
parent 12f2d45ee4
commit c1f5e127b5

View File

@ -106,12 +106,14 @@ class MqttUtil {
this.client.on("connect", error => { this.client.on("connect", error => {
this.connected = this.client.connected && !this.client.reconnecting; this.connected = this.client.connected && !this.client.reconnecting;
this.reconnectCount = 0; this.reconnectCount = 0;
console.log("消息连接成功:", this.client) console.log("connect:", this.client)
console.log("connect:", this.connected)
}) })
this.client.on("reconnect", error => { this.client.on("reconnect", error => {
this.connected = this.client.connected && !this.client.reconnecting; this.connected = this.client.connected && !this.client.reconnecting;
this.reconnectCount++; this.reconnectCount++;
console.log(`消息重连中${this.reconnectCount}`, this.client) console.log("reconnect:", this.client)
console.log(`reconnect_${this.reconnectCount}:`, this.connected)
}) })
this.client.on("error", error => { this.client.on("error", error => {
console.log("error", error) console.log("error", error)
@ -121,7 +123,7 @@ class MqttUtil {
console.log("message", message.toString()) console.log("message", message.toString())
this.onEventHandler(topic, message); this.onEventHandler(topic, message);
}) })
this.client.on("close", function() { this.client.on("close", () => {
console.log("已断开连接1") console.log("已断开连接1")
this.client = null; this.client = null;
this.connected = false; this.connected = false;
@ -133,8 +135,12 @@ class MqttUtil {
this.client.end(false, null, () => {}); this.client.end(false, null, () => {});
} }
//订阅主题 //订阅主题
subscribes(topic) { subscribes(topics) {
this.client.subscribe(topic, error => { if (!this.client || !this.connected) {
return;
}
console.log("subscribes", topics)
this.client.subscribe(topics, error => {
if (error) { if (error) {
console.log(`订阅主题失败:`, error) console.log(`订阅主题失败:`, error)
} else { } else {
@ -143,8 +149,12 @@ class MqttUtil {
}) })
} }
//取消订阅 //取消订阅
unsubscribes(topic) { unsubscribes(topics) {
this.client.unsubscribe(topic, error => { if (!this.client || !this.connected) {
return;
}
console.log("unsubscribes", topics)
this.client.unsubscribe(topics, error => {
if (!error) { if (!error) {
console.log("取消订阅成功") console.log("取消订阅成功")
} else { } else {
@ -169,36 +179,44 @@ class MqttUtil {
// 先连接mqtt // 先连接mqtt
connect(this.options, (res) => { connect(this.options, (res) => {
console.log("connect", res); console.log("connect", res);
if (res.code == 200) { // if (res.code == 200) {}
this.connected = isConnected(); this.connected = isConnected();
this.reconnectCount = 0; this.reconnectCount = 0;
console.log("连接成功:", this.connected) console.log("connect:", this.connected)
}
}) })
// 监听连接丢失函数 // 监听连接丢失函数
onConnectLost((res) => { onConnectLost((res) => {
console.log("连接丢失的原因:", res); console.log("onConnectLost", res);
this.connected = false; this.connected = isConnected();
console.log("onConnectLost:", this.connected)
}) })
// 监听自动重新连接函数 // 监听自动重新连接函数
onReconnect((res) => { onReconnect((res) => {
console.log("消息重连中:", res); console.log("onReconnect", res);
// this.connected = this.client.connected && !this.client.reconnecting;
this.connected = isConnected(); this.connected = isConnected();
this.reconnectCount++; this.reconnectCount++;
console.log(`消息重连中${this.reconnectCount}:`, this.connected) console.log("onConnectLost_${this.reconnectCount}:", this.connected)
}) })
} }
//结束链接 //结束链接
over() { over() {
if (!isConnected()) {
return;
}
disConnect((res) => { disConnect((res) => {
console.log("接受消息:", res) console.log("接受消息:", res)
this.connected = isConnected(); if (res.code == 200) {
this.reconnectCount = 0; this.reconnectCount = 0;
this.connected = isConnected();
}
}) })
} }
//订阅主题 //订阅主题
subscribes(topics) { subscribes(topics) {
if (!isConnected()) {
return;
}
console.log("subscribes", topics)
if (topics && topics.length) { if (topics && topics.length) {
topics.forEach(x => { topics.forEach(x => {
subscribe(x, 0, (res) => { subscribe(x, 0, (res) => {
@ -211,6 +229,10 @@ class MqttUtil {
} }
//取消订阅 //取消订阅
unsubscribes(topics) { unsubscribes(topics) {
if (!isConnected()) {
return;
}
console.log("unsubscribes", topics)
if (topics && topics.length) { if (topics && topics.length) {
topics.forEach(x => { topics.forEach(x => {
unSubscribe(x, (res) => {}) unSubscribe(x, (res) => {})