Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for weapp #653

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ coverage
test/typescript/.idea/*
test/typescript/*.js
test/typescript/*.map
package-lock.json
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
"mocha": true,
"indent": 2,
"latedef": true,
"immed": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was defined multi times. see line 4

"shadow": false
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ in JavaScript for node.js and the browser.
* [Command Line Tools](#cli)
* [API](#api)
* [Browser](#browser)
* [Weapp](#weapp)
* [About QoS](#qos)
* [TypeScript](#typescript)
* [Contributing](#contributing)
Expand Down Expand Up @@ -433,6 +434,27 @@ The MQTT.js bundle is available through http://unpkg.com, specifically
at https://unpkg.com/mqtt/dist/mqtt.min.js.
See http://unpkg.com for the full documentation on version ranges.

<a name="weapp"></a>
## Wexin App
Surport [Weixin App](https://mp.weixin.qq.com/). See [Doc](https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-socket.html).
<a name="example"></a>
## Example
```js
var mqtt = require('mqtt')
var client = mqtt.connect('wxs://test.mosquitto.org')

client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})

client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
```

<a name="browserify"></a>
### Browserify

Expand Down
300 changes: 154 additions & 146 deletions lib/connect/index.js
Original file line number Diff line number Diff line change
@@ -1,146 +1,154 @@
'use strict'

var MqttClient = require('../client')
var url = require('url')
var xtend = require('xtend')
var protocols = {}

if (process.title !== 'browser') {
protocols.mqtt = require('./tcp')
protocols.tcp = require('./tcp')
protocols.ssl = require('./tls')
protocols.tls = require('./tls')
protocols.mqtts = require('./tls')
}

protocols.ws = require('./ws')
protocols.wss = require('./ws')

/**
* Parse the auth attribute and merge username and password in the options object.
*
* @param {Object} [opts] option object
*/
function parseAuthOptions (opts) {
var matches
if (opts.auth) {
matches = opts.auth.match(/^(.+):(.+)$/)
if (matches) {
opts.username = matches[1]
opts.password = matches[2]
} else {
opts.username = opts.auth
}
}
}

/**
* connect - connect to an MQTT broker.
*
* @param {String} [brokerUrl] - url of the broker, optional
* @param {Object} opts - see MqttClient#constructor
*/
function connect (brokerUrl, opts) {
if ((typeof brokerUrl === 'object') && !opts) {
opts = brokerUrl
brokerUrl = null
}

opts = opts || {}

if (brokerUrl) {
var parsed = url.parse(brokerUrl, true)
if (parsed.port != null) {
parsed.port = Number(parsed.port)
}

opts = xtend(parsed, opts)

if (opts.protocol === null) {
throw new Error('Missing protocol')
}
opts.protocol = opts.protocol.replace(/:$/, '')
}

// merge in the auth options if supplied
parseAuthOptions(opts)

// support clientId passed in the query string of the url
if (opts.query && typeof opts.query.clientId === 'string') {
opts.clientId = opts.query.clientId
}

if (opts.cert && opts.key) {
if (opts.protocol) {
if (['mqtts', 'wss'].indexOf(opts.protocol) === -1) {
/*
* jshint and eslint
* complains that break from default cannot be reached after throw
* it is a foced exit from a control structure
* maybe add a check after switch to see if it went through default
* and then throw the error
*/
/* jshint -W027 */
/* eslint no-unreachable:1 */
switch (opts.protocol) {
case 'mqtt':
opts.protocol = 'mqtts'
break
case 'ws':
opts.protocol = 'wss'
break
default:
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
break
}
/* eslint no-unreachable:0 */
/* jshint +W027 */
}
} else {
// don't know what protocol he want to use, mqtts or wss
throw new Error('Missing secure protocol key')
}
}

if (!protocols[opts.protocol]) {
var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
opts.protocol = [
'mqtt',
'mqtts',
'ws',
'wss'
].filter(function (key, index) {
if (isSecure && index % 2 === 0) {
// Skip insecure protocols when requesting a secure one.
return false
}
return (typeof protocols[key] === 'function')
})[0]
}

if (opts.clean === false && !opts.clientId) {
throw new Error('Missing clientId for unclean clients')
}

function wrapper (client) {
if (opts.servers) {
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
client._reconnectCount = 0
}

opts.host = opts.servers[client._reconnectCount].host
opts.port = opts.servers[client._reconnectCount].port
opts.hostname = opts.host

client._reconnectCount++
}

return protocols[opts.protocol](client, opts)
}

return new MqttClient(wrapper, opts)
}

module.exports = connect
module.exports.connect = connect
module.exports.MqttClient = MqttClient
'use strict'

var MqttClient = require('../client')
var url = require('url')
var xtend = require('xtend')
var protocols = {}

if (process.title !== 'browser') {
protocols.mqtt = require('./tcp')
protocols.tcp = require('./tcp')
protocols.ssl = require('./tls')
protocols.tls = require('./tls')
protocols.mqtts = require('./tls')
} else {
protocols.wx = require('./wx')
protocols.wxs = require('./wx')
}

protocols.ws = require('./ws')
protocols.wss = require('./ws')

/**
* Parse the auth attribute and merge username and password in the options object.
*
* @param {Object} [opts] option object
*/
function parseAuthOptions (opts) {
var matches
if (opts.auth) {
matches = opts.auth.match(/^(.+):(.+)$/)
if (matches) {
opts.username = matches[1]
opts.password = matches[2]
} else {
opts.username = opts.auth
}
}
}

/**
* connect - connect to an MQTT broker.
*
* @param {String} [brokerUrl] - url of the broker, optional
* @param {Object} opts - see MqttClient#constructor
*/
function connect (brokerUrl, opts) {
if ((typeof brokerUrl === 'object') && !opts) {
opts = brokerUrl
brokerUrl = null
}

opts = opts || {}

if (brokerUrl) {
var parsed = url.parse(brokerUrl, true)
if (parsed.port != null) {
parsed.port = Number(parsed.port)
}

opts = xtend(parsed, opts)

if (opts.protocol === null) {
throw new Error('Missing protocol')
}
opts.protocol = opts.protocol.replace(/:$/, '')
}

// merge in the auth options if supplied
parseAuthOptions(opts)

// support clientId passed in the query string of the url
if (opts.query && typeof opts.query.clientId === 'string') {
opts.clientId = opts.query.clientId
}

if (opts.cert && opts.key) {
if (opts.protocol) {
if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
/*
* jshint and eslint
* complains that break from default cannot be reached after throw
* it is a foced exit from a control structure
* maybe add a check after switch to see if it went through default
* and then throw the error
*/
/* jshint -W027 */
/* eslint no-unreachable:1 */
switch (opts.protocol) {
case 'mqtt':
opts.protocol = 'mqtts'
break
case 'ws':
opts.protocol = 'wss'
break
case 'wx':
opts.protocol = 'wxs'
break
default:
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
break
}
/* eslint no-unreachable:0 */
/* jshint +W027 */
}
} else {
// don't know what protocol he want to use, mqtts or wss
throw new Error('Missing secure protocol key')
}
}

if (!protocols[opts.protocol]) {
var isSecure = ['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) !== -1
opts.protocol = [
'mqtt',
'mqtts',
'ws',
'wss',
'wx',
'wxs'
].filter(function (key, index) {
if (isSecure && index % 2 === 0) {
// Skip insecure protocols when requesting a secure one.
return false
}
return (typeof protocols[key] === 'function')
})[0]
}

if (opts.clean === false && !opts.clientId) {
throw new Error('Missing clientId for unclean clients')
}

function wrapper (client) {
if (opts.servers) {
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
client._reconnectCount = 0
}

opts.host = opts.servers[client._reconnectCount].host
opts.port = opts.servers[client._reconnectCount].port
opts.hostname = opts.host

client._reconnectCount++
}

return protocols[opts.protocol](client, opts)
}

return new MqttClient(wrapper, opts)
}

module.exports = connect
module.exports.connect = connect
module.exports.MqttClient = MqttClient
Loading