0.1.9 bind end to close for consistency with rest of UCI

master
David Kebler 2019-03-17 13:50:04 -07:00
parent 3788a4c097
commit 4031ec9e02
3 changed files with 9 additions and 4 deletions

View File

@ -9,7 +9,7 @@ let mqtt = new Client({id:'example-mqtt-client', topics:'lights'})
console.log(await mqtt.connect())
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.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'})
// now can send via some mqtt client

View File

@ -1,6 +1,6 @@
{
"name": "@uci/mqtt",
"version": "0.1.8",
"version": "0.1.9",
"description": "mqtt client with json payloads and mqtt custom broker",
"main": "src",
"scripts": {
@ -26,7 +26,7 @@
"homepage": "https://github.com/uCOMmandIt/uci-changeme#readme",
"dependencies": {
"@uci-utils/logger": "0.0.13",
"async-mqtt": "^2.2.0",
"async-mqtt": "^1.0",
"better-try-catch": "^0.6.2",
"is-plain-object": "^2.0.4",
"lodash.merge": "^4.6.1",

View File

@ -1,4 +1,5 @@
import { connect } from 'async-mqtt'
import { promisify } from 'util'
import merge from 'lodash.merge'
import btc from 'better-try-catch'
import union from 'lodash.union'
@ -28,7 +29,7 @@ class MQTTClient {
name: 'mqtt',
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
this.topics = Array.isArray(opts.topics)
? opts.topics
@ -52,7 +53,10 @@ class MQTTClient {
this.url,
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.close = promisify(this.end).bind(this)
let timeout = this.opts.connectTimeout || 5000
setTimeout(() => {
reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts})
@ -213,6 +217,7 @@ class MQTTClient {
temp[func] = this[func]
})
merge(this, obj)
funcs.forEach(func => {
this['_' + func] = this[func]
this[func] = temp[func]