mqtt stuff added

This commit is contained in:
2018-05-16 10:44:10 +02:00
parent 74584cdbbe
commit c7eb46b346
499 changed files with 55775 additions and 19 deletions

132
node_modules/mqtt/test/browser/server.js generated vendored Normal file
View File

@ -0,0 +1,132 @@
'use strict'
var handleClient
var websocket = require('websocket-stream')
var WebSocketServer = require('ws').Server
var Connection = require('mqtt-connection')
var http = require('http')
handleClient = function (client) {
var self = this
if (!self.clients) {
self.clients = {}
}
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
self.clients[packet.clientId] = client
client.subscriptions = []
})
client.on('publish', function (packet) {
var i, k, c, s, publish
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
for (k in self.clients) {
c = self.clients[k]
publish = false
for (i = 0; i < c.subscriptions.length; i++) {
s = c.subscriptions[i]
if (s.test(packet.topic)) {
publish = true
}
}
if (publish) {
try {
c.publish({topic: packet.topic, payload: packet.payload})
} catch (error) {
delete self.clients[k]
}
}
}
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
var qos
var topic
var reg
var granted = []
for (var i = 0; i < packet.subscriptions.length; i++) {
qos = packet.subscriptions[i].qos
topic = packet.subscriptions[i].topic
reg = new RegExp(topic.replace('+', '[^/]+').replace('#', '.+') + '$')
granted.push(qos)
client.subscriptions.push(reg)
}
client.suback({messageId: packet.messageId, granted: granted})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
}
function start (startPort, done) {
var server = http.createServer()
var wss = new WebSocketServer({server: server})
wss.on('connection', function (ws) {
var stream, connection
if (!(ws.protocol === 'mqtt' ||
ws.protocol === 'mqttv3.1')) {
return ws.close()
}
stream = websocket(ws)
connection = new Connection(stream)
handleClient.call(server, connection)
})
server.listen(startPort, done)
server.on('request', function (req, res) {
res.statusCode = 404
res.end('Not Found')
})
return server
}
if (require.main === module) {
start(process.env.PORT || process.env.ZUUL_PORT, function (err) {
if (err) {
console.error(err)
return
}
console.log('tunnelled server started on port', process.env.PORT || process.env.ZUUL_PORT)
})
}

92
node_modules/mqtt/test/browser/test.js generated vendored Normal file
View File

@ -0,0 +1,92 @@
'use strict'
var mqtt = require('../../lib/connect')
var _URL = require('url')
var xtend = require('xtend')
var parsed = _URL.parse(document.URL)
var isHttps = parsed.protocol === 'https:'
var port = parsed.port || (isHttps ? 443 : 80)
var host = parsed.hostname
var protocol = isHttps ? 'wss' : 'ws'
function clientTests (buildClient) {
var client
beforeEach(function () {
client = buildClient()
client.on('offline', function () {
console.log('client offline')
})
client.on('connect', function () {
console.log('client connect')
})
client.on('reconnect', function () {
console.log('client reconnect')
})
})
afterEach(function (done) {
client.once('close', function () {
done()
})
client.end()
})
it('should connect', function (done) {
client.on('connect', function () {
done()
})
})
it('should publish and subscribe', function (done) {
client.subscribe('hello', function () {
done()
}).publish('hello', 'world')
})
}
function suiteFactory (configName, opts) {
function setVersion (base) {
return xtend(base || {}, opts)
}
var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
describe(suiteName, function () {
this.timeout(10000)
describe('specifying nothing', function () {
clientTests(function () {
return mqtt.connect(setVersion())
})
})
if (parsed.hostname === 'localhost') {
describe('specifying a port', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port }))
})
})
}
describe('specifying a port and host', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
})
})
describe('specifying a URL', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
})
})
describe('specifying a URL with a path', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
})
})
})
}
suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
suiteFactory('default', {})

93
node_modules/mqtt/test/browser/wx.js generated vendored Normal file
View File

@ -0,0 +1,93 @@
'use strict'
var mqtt = require('../../lib/connect')
var _URL = require('url')
var xtend = require('xtend')
var parsed = _URL.parse(document.URL)
var isHttps = parsed.protocol === 'https:'
var port = parsed.port || (isHttps ? 443 : 80)
var host = parsed.hostname
var protocol = isHttps ? 'wxs' : 'wx'
require('../helpers/wx')
function clientTests (buildClient) {
var client
beforeEach(function () {
client = buildClient()
client.on('offline', function () {
console.log('client offline')
})
client.on('connect', function () {
console.log('client connect')
})
client.on('reconnect', function () {
console.log('client reconnect')
})
})
afterEach(function (done) {
client.once('close', function () {
done()
})
client.end()
})
it('should connect', function (done) {
client.on('connect', function () {
done()
})
})
it('should publish and subscribe', function (done) {
client.subscribe('hello', function () {
done()
}).publish('hello', 'world')
})
}
function suiteFactory (configName, opts) {
function setVersion (base) {
return xtend(base || {}, opts)
}
var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
describe(suiteName, function () {
this.timeout(10000)
describe('specifying nothing', function () {
clientTests(function () {
return mqtt.connect(setVersion())
})
})
if (parsed.hostname === 'localhost') {
describe('specifying a port', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port }))
})
})
}
describe('specifying a port and host', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
})
})
describe('specifying a URL', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
})
})
describe('specifying a URL with a path', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
})
})
})
}
suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
suiteFactory('default', {})