修复:MQTT能连接成功但是不能正常工作的问题 #310

This commit is contained in:
pppscn 2023-07-07 13:56:12 +08:00
parent a9eaa8c791
commit 6b6f8ecfa5
4 changed files with 14 additions and 13 deletions

View File

@ -192,7 +192,7 @@ dependencies {
//kmnkt基于Kotlin Multiplatform的跨平台socket通信统一接口UDP/TCP/MQTT协议 //kmnkt基于Kotlin Multiplatform的跨平台socket通信统一接口UDP/TCP/MQTT协议
//https://github.com/xuankaicat/kmnkt //https://github.com/xuankaicat/kmnkt
implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5") implementation("org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5")
implementation files('libs/socket.aar') implementation files('libs/socket-2.0.0-alpha06-2.aar')
testImplementation deps.junit testImplementation deps.junit
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,7 @@ package com.idormy.sms.forwarder.utils.sender
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.text.TextUtils import android.text.TextUtils
import android.util.Base64 import android.util.Base64
import android.util.Log
import com.gitee.xuankaicat.kmnkt.socket.MqttQuality import com.gitee.xuankaicat.kmnkt.socket.MqttQuality
import com.gitee.xuankaicat.kmnkt.socket.dsl.mqtt import com.gitee.xuankaicat.kmnkt.socket.dsl.mqtt
import com.gitee.xuankaicat.kmnkt.socket.dsl.tcp import com.gitee.xuankaicat.kmnkt.socket.dsl.tcp
@ -81,34 +82,34 @@ class SocketUtils {
socket.open { socket.open {
success { success {
//开启连接成功时执行 Log.d(TAG, "${setting.method}连接成功")
isConnected = true isConnected = true
SendUtils.updateLogs(logId, 1, "TCP连接成功") //SendUtils.updateLogs(logId, 1, "TCP连接成功")
socket.send(message) socket.send(message)
socket.startReceive { str, data -> socket.startReceive { str, data ->
isReceived = true isReceived = true
android.util.Log.d(TAG, "str=$str,data=$data") Log.d(TAG, "str=$str,data=$data")
SendUtils.updateLogs(logId, 2, "收到订阅消息str=$str,data=$data") SendUtils.updateLogs(logId, 2, "收到订阅消息str=$str,data=$data")
SendUtils.senderLogic(2, msgInfo, rule, senderIndex, msgId) SendUtils.senderLogic(2, msgInfo, rule, senderIndex, msgId)
return@startReceive false return@startReceive false
} }
} }
failure { failure {
//开启连接失败时执行 Log.d(TAG, "${setting.method}连接失败")
val status = 0 val status = 0
SendUtils.updateLogs(logId, status, "TCP连接失败") SendUtils.updateLogs(logId, status, "TCP连接失败")
SendUtils.senderLogic(status, msgInfo, rule, senderIndex, msgId) SendUtils.senderLogic(status, msgInfo, rule, senderIndex, msgId)
return@failure false//是否继续尝试连接 return@failure false//是否继续尝试连接
} }
loss { loss {
//失去连接时执行 Log.d(TAG, "${setting.method}连接断开")
return@loss false//是否尝试重连 return@loss true//是否尝试重连
} }
} }
//延时5秒关闭连接 //延时5秒关闭连接
if (isConnected) { if (isConnected) {
Thread.sleep(5000) Thread.sleep(10000)
socket.stopReceive() socket.stopReceive()
socket.close() socket.close()
if (!isReceived) { if (!isReceived) {
@ -141,8 +142,8 @@ class SocketUtils {
mqtt.open { mqtt.open {
success { success {
//开启连接成功时执行 Log.d(TAG, "MQTT连接成功")
SendUtils.updateLogs(logId, 1, "MQTT连接成功") //SendUtils.updateLogs(logId, 1, "MQTT连接成功")
// 订阅并发布后等待至拿到响应消息并赋值给result // 订阅并发布后等待至拿到响应消息并赋值给result
// 如果超过10秒没有收到消息则将result设为"消息响应超时"并取消订阅topic // 如果超过10秒没有收到消息则将result设为"消息响应超时"并取消订阅topic
val response = mqtt.sendAndReceiveSync(setting.outMessageTopic, setting.inMessageTopic, message, 10000L) ?: "消息响应超时" val response = mqtt.sendAndReceiveSync(setting.outMessageTopic, setting.inMessageTopic, message, 10000L) ?: "消息响应超时"
@ -154,15 +155,15 @@ class SocketUtils {
return@success return@success
} }
failure { failure {
//开启连接失败时执行 Log.d(TAG, "MQTT连接失败")
val status = 0 val status = 0
SendUtils.updateLogs(logId, status, "MQTT连接失败") SendUtils.updateLogs(logId, status, "MQTT连接失败")
SendUtils.senderLogic(status, msgInfo, rule, senderIndex, msgId) SendUtils.senderLogic(status, msgInfo, rule, senderIndex, msgId)
return@failure false//是否继续尝试连接 return@failure false//是否继续尝试连接
} }
loss { loss {
//失去连接时执行 Log.d(TAG, "MQTT失去连接")
return@loss false//是否尝试重连 return@loss true//是否尝试重连
} }
} }