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