diff --git a/README.md b/README.md index ad19de2f..42a38333 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ server.listen(8883, function () { * Client * client.id * client.clean + * client.conn + * client.req * client.publish() * client.subscribe() * client.unsubscribe() @@ -385,6 +387,24 @@ The id of the client, as specified by the CONNECT packet, defaults to 'aedes_' + `true` if the client connected (CONNECT) with `clean: true`, `false` otherwise. Check the MQTT spec for what this means. +------------------------------------------------------- + +### client#conn + +The client's connection stream object. + +In the case of `net.createServer` brokers, it's the connection passed to the `connectionlistener` function by node's [net.createServer](https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) API. + +In the case of [websocket-stream](https://www.npmjs.com/package/websocket-stream) brokers, it's the `stream` argument passed to the `handle` function described in [that library's documentation](https://github.com/maxogden/websocket-stream/blob/e2a51644bb35132d7aa477ae1a27ff083fedbf08/readme.md#on-the-server). + +------------------------------------------------------- + +### client#req + +The HTTP Websocket upgrade request object passed to websocket broker's `handle` function by the [`websocket-stream` library](https://github.com/maxogden/websocket-stream/blob/e2a51644bb35132d7aa477ae1a27ff083fedbf08/readme.md#on-the-server). + +If your clients are connecting to aedes via websocket and you need access to headers or cookies, you can get them here. **NOTE:** this property is only present for websocket connections. + ------------------------------------------------------- ### client#publish(message, [callback]) diff --git a/aedes.js b/aedes.js index 96db255e..8036c135 100644 --- a/aedes.js +++ b/aedes.js @@ -40,11 +40,11 @@ function Aedes (opts) { this.counter = 0 this.connectTimeout = opts.connectTimeout this.mq = opts.mq || mqemitter(opts) - this.handle = function handle (conn) { + this.handle = function handle (conn, req) { conn.setMaxListeners(opts.concurrency * 2) // create a new Client instance for a new connection // return, just to please standard - return new Client(that, conn) + return new Client(that, conn, req) } this.persistence = opts.persistence || memory() this.persistence.broker = this diff --git a/lib/client.js b/lib/client.js index 10d574aa..b1be41c8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -14,11 +14,12 @@ var handle = require('./handlers') module.exports = Client -function Client (broker, conn) { +function Client (broker, conn, req) { var that = this this.broker = broker this.conn = conn + this.req = req this.parser = mqtt.parser() this.connected = false this.connackSent = false diff --git a/test/connect.js b/test/connect.js index 174c8aad..2a1f374e 100644 --- a/test/connect.js +++ b/test/connect.js @@ -5,6 +5,9 @@ var helper = require('./helper') var aedes = require('../') var setup = helper.setup var connect = helper.connect +var http = require('http') +var ws = require('websocket-stream') +var mqtt = require('mqtt') ;[{ ver: 3, id: 'MQIsdp' }, { ver: 4, id: 'MQTT' }].forEach(function (ele) { test('connect and connack (minimal)', function (t) { @@ -361,3 +364,48 @@ test('reject clients with wrong protocol name', function (t) { broker.on('closed', t.end.bind(t)) }) }) + +// websocket-stream based connections +test('websocket clients have access to the request object', function (t) { + t.plan(3) + + var broker = aedes() + var server = http.createServer() + ws.createServer({ + server: server + }, broker.handle) + + server.listen(4883, function (err) { + t.error(err, 'no error') + }) + + broker.on('client', function (client) { + if (client.req) { + t.pass('client request object present') + if (client.req.headers) { + t.equal('sample', client.req.headers['x-test-protocol']) + finish() + } + } else { + t.fail('no request object present') + } + }) + + var client = mqtt.connect('ws://localhost:4883', { + wsOptions: { + headers: { + 'X-Test-Protocol': 'sample' + } + } + }) + + var timer = setTimeout(finish, 1000) + + function finish () { + clearTimeout(timer) + broker.close() + server.close() + client.end() + t.end() + } +})