Created
July 9, 2018 09:26
-
-
Save chartinger/aeb8859d49b5a092c7cd13e1b797d2c3 to your computer and use it in GitHub Desktop.
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
/* | |
Simple test for Mosca retain flag | |
- Creates a Mosca server | |
- Connects mqtt.js client when ready | |
- sends a random message with retain flag | |
- subscribes to the topic after sending | |
- checks if received message is the correct one | |
- checks ifd retain flag is set | |
Sample Output: | |
Received correct random message with value 178495 | |
Packet { | |
cmd: 'publish', | |
retain: false, <---- should be true | |
qos: 0, | |
dup: false, | |
length: 13, | |
topic: '/test', | |
payload: <Buffer 31 37 38 34 39 35> } | |
*/ | |
const Mosca = require('mosca') | |
const MQTT = require('mqtt') | |
let client = null | |
let url = 'ws://localhost:1884' | |
// To test with mosquitto: | |
// let url = 'ws://test.mosquitto.org:8080' | |
const server= new Mosca.Server({ | |
http: { | |
port: 1884 | |
}, | |
persistence: { | |
factory: Mosca.persistence.Memory | |
} | |
}); | |
server.on('ready', () => { | |
let randomMessage = '' + Math.floor((Math.random() * 1000000)); | |
client = MQTT.connect(url) | |
client.on('connect', () => { | |
client.on('message', (topic, message, packet) => { | |
if (randomMessage === message.toString()) { | |
console.log('Received correct random message with value ' + message.toString()) | |
} | |
console.log(packet); | |
client.end() | |
server.close() | |
}) | |
client.publish('/test', randomMessage, { retain: true }) | |
setTimeout(() => { | |
client.subscribe('/test') | |
}, 1000) | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment