- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果连接丢失,我的 paho-mqtt 服务无法重新连接到代理。
在连接丢失时,我使用 adb shell 从 Android 客户端 ping 两个代理,并从托管 mosquito 代理的服务器 (Windows 10) 对 Android 设备执行 ping 操作。
我通过部署了 Android Things(最新版本)的 Raspberry Pi 3B 使用 LAN 连接到网络。 LAN网络相当稳定。对于 Paho MQTT,我使用的是最新版本。
我注意到我的 MQTT 连接随机丢失。
fun connect(context: Context) {
connectOptions.keepAliveInterval=30//seconds
connectOptions.mqttVersion = MqttConnectOptions.MQTT_VERSION_3_1_1
connectOptions.isAutomaticReconnect = true
connectOptions.isCleanSession = false
connectOptions.setWill(Global.HmiSrNo + "_out", "Disconnected".toByteArray(), 2, false)
mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId)
try {
val token = mqttAndroidClient.connect(connectOptions)
token.actionCallback = object : IMqttActionListener {
override fun onSuccess(asyncActionToken: IMqttToken)
{
subscribe(context)
automicLight.set(true)
issnackbarshown = true
if(Global.connectivitylost)
wantToCloseDialog = true
}
override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
//connectionStatus = false
Log.i("Connection", "failure")
// Give your callback on connection failure here
exception.printStackTrace()
}
}
} catch (e: MqttException) {
// Give your callback on connection failure here
e.printStackTrace()
}
}
建筑等级
<service android:name="org.eclipse.paho.android.service.MqttService" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
最佳答案
以下是 JAVA,因此您需要进行 kotlin 转换。
如果没有错误消息,将很难调试。从 MqttCallback 接口(interface)覆盖“connectionLost(Throwable cause)”。这将为您提供错误原因。
在我的应用程序中,我在一个类中实现了 IMqttActionListener 和 MqttCallback。跟踪应用程序是否调用断开连接或因其他原因断开连接。如果应用程序没有调用 disconnect 重新启动连接过程。我过去看到的最常见原因之一是 Context 为 null,它会关闭连接并且不允许重新启动连接。如果使用 AndroidViewModel,您可以使用应比 Activity Context 生命周期更长的应用程序上下文。
public class Pg3MqttDelegate implements IMqttActionListener, MqttCallback {
....
/////////////////////// IMqttActionListener /////////////////////////////
////////////////////////////////////////////////////////////////////////
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.v(LOG_TAG, "onSuccess: " + asyncActionToken);
viewModel.setMqttConnected(true);
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
viewModel.setError(ErrorObject.getInstance("" + exception));
Log.v(LOG_TAG, "onFailure: " + exception + ", " + asyncActionToken.getException());
}
/////////////////////////// MqttCallback ///////////////////////////////
////////////////////////////////////////////////////////////////////////
@Override
public void connectionLost(Throwable cause) {
Log.v(LOG_TAG, "connection lost: " + cause);
viewModel.setMqttConnected(false);
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.v(LOG_TAG, "Id:" + message.getId() + ", message topic:" + topic + ", message:" + message.toString());
....
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//triggered when publish is completed
try {
Log.v(LOG_TAG, "deliveryComplete token:" + token.getMessage());
} catch (MqttException e) {
Log.v(LOG_TAG, "could not log deliveryComplete message");
}
}
关于android - MQTT Paho 客户端不会自动重新连接到代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69615728/
当我使用 paho 和 spring 框架修复应用程序中客户端的一些问题时,我发现了 spring MQTT 集成。我不确定这是否能解决我的问题,我什至不确定这是什么。 有人可以解释一下两者一起使用有
我正在尝试设置一个 mqtt 客户端,但我收到一个 ImportError: 我安装了 paho: pip install paho-mqtt 它说:成功安装 dnspython-1.15.0 pah
我的 raspberrypi 上的以下 python 代码没有连接到我的 mqtt 代理,它只是在打印 Connecting... 后挂起: import paho.mqtt.client as mq
当我通过 MQTTLens 测试发布时,它有效。然而,当我按下按钮时,它确实会触发“on_publish”,但另一端的 on_message 没有收到任何内容;它没有被触发。有两个 Raspberry
我正在使用 Java Paho MQTT 客户端库通过 MQTT 连接到服务器。 库支持使用签名证书进行身份验证和加密的 TLS/SSL。 但是,如果我只是使用用户名和密码进行身份验证(如下所示),而
我的 python paho-mqtt client无法连接到用 java 编写的代理。代理已使用 jks 类型证书启用 SSL 连接。经纪人不在我的管理范围内。 我将 jks 证书转换为 pem 证
如果连接丢失,我的 paho-mqtt 服务无法重新连接到代理。 在连接丢失时,我使用 adb shell 从 Android 客户端 ping 两个代理,并从托管 mosquito 代理的服务器 (
我在笔记本电脑上运行 MQTT Mosquitto 代理。然后我尝试将 2 个 Paho MQTT 客户端连接到它:1)使用 Java Paho 从 Android 手机和 2)使用 Python
有没有一种优雅的方法可以从代理中提取一条消息而不必: 订阅 创建一个 on_message() 接收消息 退订 我问是因为我们使用了一个包含多个字段的 json 消息。当新数据进入时,我只想更新 js
我正在使用适用于 Android 的 Paho MQTT 库,我的应用程序正在交换实时编辑数据。我想在应用程序与代理断开连接时禁用编辑文本,并在重新建立连接时启用它。 第一部分很简单,MqttCall
我正在使用 Eclipse MQTT 库 Paho,但我找不到方法来决定客户端与代理的连接何时建立。 有人知道目前是否有办法做到这一点吗? 我似乎在文档中的 MqttCallback 类中找不到任
我对 paho mqtt 库有一个小问题。我注册了回调函数 MQTTClient_messageArrived 和 MQTTClient_connectionLost。 我在此回调函数中调用 MQTT
我使用 Apache Artemis 作为我的 mqtt 代理。我有一个以主从方式配置的 Artemis 集群。当主服务器发生故障时,备份服务器接管,客户端必须连接到从服务器。主站和从站有不同的IP。
我目前正在开发一个应该在医疗机构中运行的小型“紧急按钮”应用程序。由于该项目的假设之一是独立于 Internet,因此我决定使用在本地 WLAN 中设置服务器的 MQTT。 我已经实现了 Paho A
我正在尝试使用基本的 Eclipse Paho MQTT 客户端版本 1.1.0,用于连接到 CloudAMQP RabbitMQ 实例、订阅主题并接收消息(我从 Web 管理控制台发送的消息)。 如
我已经创建了 mqtt 连接suscribeData(),如下所示,以 24/7 从多个设备接收数据 @RequestMapping("/suscribe") @ResponseBody public
我最近开始使用 paho mqtt for java 和 mqtt,但我坚持使用 mqtt 代理和 paho 客户端本身提供的持久性机制。也许我误解了(可能就是这样)mqtt 上下文中的持久性概念。
我是 MQTT 和树莓派的新手!我正在运行一个客户端脚本,我只是使用另一个脚本发布一条消息。我正在使用自己的 Mosquitto 代理。 客户: import paho.mqtt.client as
我设法在我的 RPi 中的 Paho-MQTT 客户端和我的 VPS 中的 MQTT 代理之间建立了连接。 现在我正在尝试保护 MQTT 连接,并且我已将用户和密码添加到代理。我更改了 mosquit
为了演示 Paho MQTT,我下载了一个 Java 示例。 public class Thermometer { public static final String BROKER_URL
我是一名优秀的程序员,十分优秀!