-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: keepalive causes a reconnect loop when connection is lost
Fixes #1778
- Loading branch information
1 parent
44a2f2f
commit 945c41b
Showing
3 changed files
with
53 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
import mqtt from '.' | ||
|
||
const client = mqtt.connect('mqtt://test.mosquitto.org') | ||
const client = mqtt.connect('mqtt://test.mosquitto.org', { | ||
keepalive: 10, | ||
reconnectPeriod: 15000, | ||
}) | ||
|
||
const testTopic = 'presence' | ||
|
||
client.subscribe(testTopic, (err) => { | ||
if (!err) { | ||
console.log('subscribed to', testTopic) | ||
client.publish(testTopic, 'Hello mqtt', (err2) => { | ||
function publish() { | ||
client.publish( | ||
testTopic, | ||
`Hello mqtt ${new Date().toISOString()}`, | ||
(err2) => { | ||
if (!err2) { | ||
console.log('message published') | ||
} else { | ||
console.error(err2) | ||
} | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
client.subscribe(testTopic, (err) => { | ||
if (!err) { | ||
console.log('subscribed to', testTopic) | ||
} else { | ||
console.error(err) | ||
} | ||
}) | ||
|
||
client.on('message', (topic, message) => { | ||
console.log('received message "%s" from topic "%s"', message, topic) | ||
client.end() | ||
setTimeout(() => { | ||
publish() | ||
}, 2000) | ||
}) | ||
|
||
client.on('error', (err) => { | ||
console.error(err) | ||
}) | ||
|
||
client.on('connect', () => { | ||
console.log('connected') | ||
publish() | ||
}) | ||
|
||
client.on('disconnect', () => { | ||
console.log('disconnected') | ||
}) | ||
|
||
client.on('offline', () => { | ||
console.log('offline') | ||
}) | ||
|
||
client.on('reconnect', () => { | ||
console.log('reconnect') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters