重新连接失败:paho mqtt 使用 javascript
Reconnection failed : paho mqtt using javascript
当连接断开时,会调用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});
}
}
Error: Unknown property, reconnectInterval. Valid properties are:
timeout userName password willMessage keepAliveInterval cleanSession
useSSL invocationContext onSuccess onFailure hosts ports reconnect
mqttVersion mqttVersionExplicit 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
当连接断开时,会调用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});
}
}
Error: Unknown property, reconnectInterval. Valid properties are: timeout userName password willMessage keepAliveInterval cleanSession useSSL invocationContext onSuccess onFailure hosts ports reconnect mqttVersion mqttVersionExplicit 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