0.1.9 bind end to close for consistency with rest of UCI
parent
3788a4c097
commit
4031ec9e02
|
@ -9,7 +9,7 @@ let mqtt = new Client({id:'example-mqtt-client', topics:'lights'})
|
||||||
|
|
||||||
console.log(await mqtt.connect())
|
console.log(await mqtt.connect())
|
||||||
await mqtt.send({cmd:'lights/on', status:'sent to topics lights'})
|
await mqtt.send({cmd:'lights/on', status:'sent to topics lights'})
|
||||||
// await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'})
|
await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'})
|
||||||
// await mqtt.subscribe('topic2')
|
// await mqtt.subscribe('topic2')
|
||||||
// await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
|
// await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
|
||||||
// now can send via some mqtt client
|
// now can send via some mqtt client
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/mqtt",
|
"name": "@uci/mqtt",
|
||||||
"version": "0.1.8",
|
"version": "0.1.9",
|
||||||
"description": "mqtt client with json payloads and mqtt custom broker",
|
"description": "mqtt client with json payloads and mqtt custom broker",
|
||||||
"main": "src",
|
"main": "src",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@uci-utils/logger": "0.0.13",
|
"@uci-utils/logger": "0.0.13",
|
||||||
"async-mqtt": "^2.2.0",
|
"async-mqtt": "^1.0",
|
||||||
"better-try-catch": "^0.6.2",
|
"better-try-catch": "^0.6.2",
|
||||||
"is-plain-object": "^2.0.4",
|
"is-plain-object": "^2.0.4",
|
||||||
"lodash.merge": "^4.6.1",
|
"lodash.merge": "^4.6.1",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { connect } from 'async-mqtt'
|
import { connect } from 'async-mqtt'
|
||||||
|
import { promisify } from 'util'
|
||||||
import merge from 'lodash.merge'
|
import merge from 'lodash.merge'
|
||||||
import btc from 'better-try-catch'
|
import btc from 'better-try-catch'
|
||||||
import union from 'lodash.union'
|
import union from 'lodash.union'
|
||||||
|
@ -28,7 +29,7 @@ class MQTTClient {
|
||||||
name: 'mqtt',
|
name: 'mqtt',
|
||||||
id: this.id
|
id: this.id
|
||||||
})
|
})
|
||||||
this.url = opts.url || ''
|
this.url = opts.url || null
|
||||||
// subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs
|
// subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs
|
||||||
this.topics = Array.isArray(opts.topics)
|
this.topics = Array.isArray(opts.topics)
|
||||||
? opts.topics
|
? opts.topics
|
||||||
|
@ -52,7 +53,10 @@ class MQTTClient {
|
||||||
this.url,
|
this.url,
|
||||||
this.opts
|
this.opts
|
||||||
)
|
)
|
||||||
|
// version 2.0 and up messes this up. Need to look at another way
|
||||||
|
// maybe better not to merge but have mqtt client be a property of class
|
||||||
this._extend(mqtt, 'subscribe,unsubscribe') // merge mqtt client into class extend with given functions
|
this._extend(mqtt, 'subscribe,unsubscribe') // merge mqtt client into class extend with given functions
|
||||||
|
this.close = promisify(this.end).bind(this)
|
||||||
let timeout = this.opts.connectTimeout || 5000
|
let timeout = this.opts.connectTimeout || 5000
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts})
|
reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts})
|
||||||
|
@ -213,6 +217,7 @@ class MQTTClient {
|
||||||
temp[func] = this[func]
|
temp[func] = this[func]
|
||||||
})
|
})
|
||||||
merge(this, obj)
|
merge(this, obj)
|
||||||
|
|
||||||
funcs.forEach(func => {
|
funcs.forEach(func => {
|
||||||
this['_' + func] = this[func]
|
this['_' + func] = this[func]
|
||||||
this[func] = temp[func]
|
this[func] = temp[func]
|
||||||
|
|
Loading…
Reference in New Issue