当连接丢失时,将调用函数onConnectionLost,但当连接丢失后,我如何重新连接?
这是我的代码:
const client = new Paho.MQTT.Client(host, Number(port), clientID);
client.onMessageArrived = this.onMessageArrived;
client.onConnectionLost = this.onConnectionLost;
client.connect({
cleanSession : false,
onSuccess : this.onConnect,
userName: "user",
password: "pass",
onFailure : this.onConnectionLost,
keepAliveInterval: 30,
reconnect : true, // Enable automatic reconnect
reconnectInterval: 10 // Reconnect attempt interval : 10 seconds
});
onConnect = () => {
const { client } = this.state;
console.log("Connected!!!!");
this.setState({isConnected: true, error: ''});
client.subscribe(topic, qos=1);
client.subscribe(topic1, qos=1);
client.subscribe(topic2, qos=1);
};
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost : "+responseObject.errorMessage);
this.setState({error: 'Lost Connection', isConnected: false});
}
}
错误:未知属性,reconnectInterval。有效属性为:timeout用户名密码willMessage keepAliveInterval cleanSessionuseSSL invocationContext on成功on失败主机端口重新连接mqttVersion mqttVersion显式uris
使用重新连接参数
client.connect(
{
cleanSession : false,
onSuccess : onConnectSuccess,
onFailure : onFailedConnect,
keepAliveInterval: 30,
reconnect : true, // Enable automatic reconnect
}
https://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html