1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <html>
<head>
</head>
<body>
<script src="mqtt.js"></script>
<script>
const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)
const host = 'ws://127.0.0.1:9001/mqtt'
const options = {
keepalive: 60,
clientId: clientId,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
}
console.log('Connecting mqtt client')
const client = mqtt.connect(host, options)
client.on('error', (err) => {
console.log('Connection error: ', err)
client.end()
})
client.on('connect', () => {
console.log('Connected...')
client.publish("test","Hello world")
client.subscribe("test")
})
client.on('reconnect', () => {
console.log('Reconnecting...')
})
client.on('message', (topic, message, packet) => {
console.log("message reçu "+topic+":"+message)
})
</script>
</body>
</html> |
Partager