我想我会添加
keepalive: 120
到我的 Mqtt Broker 连接代码,但我仍然被 Broker 断开连接并显示以下消息(根据我的 Chrome 网络选项卡):
.M.K..HThe client was idle for too long without sending an MQTT control packet.
例如
这是我的
Angular 13
代码中的连接字符串(带有keepalive: 120
)
getBrokerConnection(): IMqttServiceOptions {
const d = new Date();
const tempId =
'HarBrowser' + (d.getUTCMinutes() + d.getUTCMilliseconds() + Math.random()).toFixed(8).toString();
const username = atob(this.config.getConfig('mqttUser'));
const password = atob(this.config.getConfig('mqttPass'));
const brokerAddr = this.config.getConfig('mqttBrokerAddress');
const port = this.config.getConfig('mqttPort');
const protocol = this.config.getConfig('mqttProtocol');
return {
hostname: brokerAddr,
port: port,
path: '/mqtt',
clean: true, // CleanSession=true does not persist the prev client session
connectTimeout: 30000,
keepalive: 120,
reconnectPeriod: 0, // 0 to disable auto-reconn (to prevent many front-end errors)
clientId: tempId,
username: username,
password: password,
protocol: protocol,
connectOnCreate: false,
protocolVersion: 5,
};
}
据我所知,
Mqtt client
负责向代理发送 pingreq
以保持连接有效(根据 https://docs.oasis-open.org/mqtt/mqtt/v5. 0/os/mqtt-v5.0-os.html#_Toc3901045)
我确实多次看到此事件记录到 Chrome 控制台,所以我假设这是我的 Mqtt 客户端根据
pingreq
参数发送该 keepalive
:
this.mqttService.onPacketsend.subscribe((res) => console.log(`onPacketsend event ${res}`));
现在我应该以编程方式发送
pingreq
,还是应该将它内置到这个Angular 包装器中到mqtt.js
?我有点不清楚我做错了什么,因为我被经纪人断开了连接。