-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ coverage | |
test/typescript/.idea/* | ||
test/typescript/*.js | ||
test/typescript/*.map | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,5 @@ | |
"mocha": true, | ||
"indent": 2, | ||
"latedef": true, | ||
"immed": true, | ||
"shadow": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this?
There was a problem hiding this comment.
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