Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cascade-examples
rigado-node-hello-world-frontend
Commits
beffe745
Commit
beffe745
authored
Apr 07, 2018
by
Vladimir Kiryakov
Browse files
fix reconnect
parent
6baa711c
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/middlewares/mqtt.js
View file @
beffe745
...
...
@@ -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.
\n
Topic: %s
\n
Payload: %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.
\n
Topic: %s
\n
Payload: %s
'
,
topic
,
message
);
if
(
_
.
isEqual
(
topic
,
msgTopic
))
{
messages
.
push
({
topic
,
message
:
message
,
timestamp
:
new
Date
().
getTime
()});
}
});
return
next
(
action
);
break
;
default
:
return
next
(
action
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment