Commit beffe745 authored by Vladimir Kiryakov's avatar Vladimir Kiryakov
Browse files

fix reconnect

parent 6baa711c
......@@ -27,30 +27,33 @@ const mqttMiddleware = (function () {
if (client != null) {
client.end();
}
url = action.payload.url;
topic = action.payload.topic;
reconnectPeriod = action.payload.reconnectPeriod;
setTimeout(() => {
url = action.payload.url;
topic = action.payload.topic;
reconnectPeriod = action.payload.reconnectPeriod;
client = mqtt.connect(url, {reconnectPeriod});
client.on('connect', () => {
if (config.debug) console.log('MQTT: client connected');
store.dispatch(connected());
client.subscribe(topic);
});
client.on('close', () => {
if (config.debug) console.log('MQTT: client disconnected');
client.end();
store.dispatch(disconnected());
});
client.on('message', (msgTopic, msg) => {
client = mqtt.connect(url, {reconnectPeriod});
client.on('connect', () => {
if (config.debug) console.log('MQTT: client connected');
store.dispatch(connected());
client.subscribe(topic);
});
client.on('close', () => {
if (config.debug) console.log('MQTT: client disconnected');
client.end();
store.dispatch(disconnected());
});
client.on('message', (msgTopic, msg) => {
const message = decoder.decode(msg);
if (config.debug) console.info('MQTT: Message recieved.\nTopic: %s\nPayload: %s', topic, message);
if (_.isEqual(topic, msgTopic)) {
messages.push({topic, message: message, timestamp: new Date().getTime()});
}
});
return next(action);
}, 100);
const message = decoder.decode(msg);
if (config.debug) console.info('MQTT: Message recieved.\nTopic: %s\nPayload: %s', topic, message);
if (_.isEqual(topic, msgTopic)) {
messages.push({topic, message: message, timestamp: new Date().getTime()});
}
});
return next(action);
break;
default:
return next(action);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment