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
49
| 'use strict'
|
| var mqtt = require('mqtt')
|
| var clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)
|
| var host = 'wss://localhost:3001/Mosca'
|
| var options = {
| keepalive: 10,
| clientId: clientId,
| protocolId: 'MQTT',
| protocolVersion: 4,
| clean: true,
| reconnectPeriod: 1000,
| connectTimeout: 30 * 1000,
| will: {
| topic: 'WillMsg',
| payload: 'Connection Closed abnormally..!',
| qos: 0,
| retain: false
| },
| username: 'demo',
| password: 'demo',
| rejectUnauthorized: false
| }
|
| var client = mqtt.connect(host, options)
|
| client.on('error', function (err) {
| console.log(err)
| client.end()
| })
|
| client.on('connect', function () {
| console.log('client connected:' + clientId)
| })
|
| client.subscribe('topic', { qos: 0 })
|
| client.publish('topic', 'wss secure connection demo...!', { qos: 0, retain: false })
|
| client.on('message', function (topic, message, packet) {
| console.log('Received Message:= ' + message.toString() + '\nOn topic:= ' + topic)
| })
|
| client.on('close', function () {
| console.log(clientId + ' disconnected')
| })
|
|