mqtt stuff added
This commit is contained in:
49
node_modules/mqtt/examples/wss/client.js
generated
vendored
Normal file
49
node_modules/mqtt/examples/wss/client.js
generated
vendored
Normal file
@ -0,0 +1,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')
|
||||
})
|
58
node_modules/mqtt/examples/wss/client_with_proxy.js
generated
vendored
Normal file
58
node_modules/mqtt/examples/wss/client_with_proxy.js
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
'use strict'
|
||||
|
||||
var mqtt = require('mqtt')
|
||||
var HttpsProxyAgent = require('https-proxy-agent')
|
||||
var url = require('url')
|
||||
/*
|
||||
host: host of the endpoint you want to connect e.g. my.mqqt.host.com
|
||||
path: path to you endpoint e.g. '/foo/bar/mqtt'
|
||||
*/
|
||||
var endpoint = 'wss://<host><path>'
|
||||
/* create proxy agent
|
||||
proxy: your proxy e.g. proxy.foo.bar.com
|
||||
port: http proxy port e.g. 8080
|
||||
*/
|
||||
var proxy = process.env.http_proxy || 'http://<proxy>:<port>'
|
||||
var parsed = url.parse(endpoint)
|
||||
var proxyOpts = url.parse(proxy)
|
||||
// true for wss
|
||||
proxyOpts.secureEndpoint = parsed.protocol ? parsed.protocol === 'wss:' : true
|
||||
var agent = new HttpsProxyAgent(proxyOpts)
|
||||
var wsOptions = {
|
||||
agent: agent
|
||||
// other wsOptions
|
||||
// foo:'bar'
|
||||
}
|
||||
var mqttOptions = {
|
||||
keepalive: 60,
|
||||
reschedulePings: true,
|
||||
protocolId: 'MQTT',
|
||||
protocolVersion: 4,
|
||||
reconnectPeriod: 1000,
|
||||
connectTimeout: 30 * 1000,
|
||||
clean: true,
|
||||
clientId: 'testClient',
|
||||
wsOptions: wsOptions
|
||||
}
|
||||
|
||||
var client = mqtt.connect(parsed, mqttOptions)
|
||||
|
||||
client.on('connect', function () {
|
||||
console.log('connected')
|
||||
})
|
||||
|
||||
client.on('error', function (a) {
|
||||
console.log('error!' + a)
|
||||
})
|
||||
|
||||
client.on('offline', function (a) {
|
||||
console.log('lost connection!' + a)
|
||||
})
|
||||
|
||||
client.on('close', function (a) {
|
||||
console.log('connection closed!' + a)
|
||||
})
|
||||
|
||||
client.on('message', function (topic, message) {
|
||||
console.log(message.toString())
|
||||
})
|
Reference in New Issue
Block a user