зеркало из
1
0
Форкнуть 0

[node] Add MQTT over Websockets support

This commit is contained in:
Pierre Cauchois 2016-10-05 18:59:53 -07:00
Родитель f338c3725d
Коммит ccaaa1b641
12 изменённых файлов: 133 добавлений и 17 удалений

Просмотреть файл

@ -8,6 +8,7 @@ var Protocol = require('azure-iot-device-amqp').Amqp;
// var Protocol = require('azure-iot-device-amqp').AmqpWs;
// var Protocol = require('azure-iot-device-http').Http;
// var Protocol = require('azure-iot-device-mqtt').Mqtt;
// var Protocol = require('azure-iot-device-mqtt').MqttWs;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;

Просмотреть файл

@ -1,12 +1,13 @@
# azure-iot-device-mqtt.Mqtt Requirements
# azure-iot-device-mqtt.Mqtt/MqttWs Requirements
## Overview
Mqtt provides a middle layer between the generic device Client and the specific MQTT transport implementation.
`Mqtt` and `MqttWs` provide a standard transport interface between the generic device Client and the specific MQTT transport implementation.
`MqttWs` will connect over secure websockets whereas `Mqtt` connects over secure TCP sockets.
## Example usage
```js
'use strict';
var Mqtt = require('azure-iot-device-mqtt').Mqtt;
var Mqtt = require('azure-iot-device-mqtt').Mqtt; // Or require('azure-iot-device-mqtt').MqttWs
var Message = require('azure-iot-device-mqtt').Message;
var config = {
@ -48,13 +49,17 @@ client.connect(function (err) {
## Public Interface
### Mqtt constructor
The `Mqtt` constructor initializes a new instance of the MQTT transport.
The `Mqtt` and `MqttWs` constructors initialize a new instance of the MQTT transport.
**SRS_NODE_DEVICE_MQTT_12_001: [** The `Mqtt` constructor shall accept the transport configuration structure.**]**
**SRS_NODE_DEVICE_MQTT_12_001: [** The constructor shall accept the transport configuration structure.**]**
**SRS_NODE_DEVICE_MQTT_12_002: [** The `Mqtt` constructor shall store the configuration structure in a member variable.**]**
**SRS_NODE_DEVICE_MQTT_12_002: [** The constructor shall store the configuration structure in a member variable.**]**
**SRS_NODE_DEVICE_MQTT_12_003: [** The `Mqtt` constructor shall create an MqttTransport object and store it in a member variable.**]**
**SRS_NODE_DEVICE_MQTT_12_003: [** The constructor shall create an MqttTransport object and store it in a member variable.**]**
**SRS_NODE_DEVICE_MQTT_16_016: [** The `Mqtt` constructor shall initialize the `uri` property of the `config` object to `mqtts://<host>`. **]**
**SRS_NODE_DEVICE_MQTT_16_017: [** The `MqttWs` constructor shall initialize the `uri` property of the `config` object to `wss://<host>:443/$iothub/websocket`. **]**
### connect(done)
The `connect` method initializes a connection to an IoT hub.

1
device/transport/mqtt/index.d.ts поставляемый
Просмотреть файл

@ -4,4 +4,5 @@
import { Client } from 'azure-iot-device';
export import Mqtt = require('./lib/mqtt');
export import MqttWs = require('./lib/mqtt_ws');
export declare function clientFromConnectionString(connectionString: string): Client;

Просмотреть файл

@ -5,6 +5,7 @@
var device = require('azure-iot-device');
var Mqtt = require('./lib/mqtt.js');
var MqttWs = require('./lib/mqtt_ws.js');
/**
* The `azure-iot-device-mqtt` module provides support the MQTT protocol to the device [client]{@link module:azure-iot-device.Client} using secure sockets.
@ -15,6 +16,7 @@ var Mqtt = require('./lib/mqtt.js');
module.exports = {
Mqtt: Mqtt,
MqttWs: MqttWs,
clientFromConnectionString: function (connectionString) {
return device.Client.fromConnectionString(connectionString, Mqtt);
}

Просмотреть файл

@ -15,13 +15,14 @@ var util = require('util');
*
* @param {Object} config Configuration object derived from the connection string by the client.
*/
/*
Codes_SRS_NODE_DEVICE_HTTP_12_001: [The Mqtt shall accept the transport configuration structure
Codes_SRS_NODE_DEVICE_HTTP_12_002: [The Mqtt shall store the configuration structure in a member variable
*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_001: [The constructor shall accept the transport configuration structure.]*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_002: [The constructor shall store the configuration structure in a member variable.]*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_003: [The constructor shall create an base transport object and store it in a member variable.]*/
function Mqtt(config) {
EventEmitter.call(this);
this._config = config;
/*Codes_SRS_NODE_DEVICE_MQTT_16_016: [The `Mqtt` constructor shall initialize the `uri` property of the `config` object to `mqtts://<host>`.]*/
this._config.uri = "mqtts://" + config.host;
this._mqtt = new Base();
}

Просмотреть файл

@ -38,7 +38,7 @@ MqttBase.prototype.connect = function (config, done) {
}
this._receiver = null;
this._hostName = 'mqtts://' + config.host;
var uri = config.uri || 'mqtts://' + config.host;
this._topic_publish = "devices/" + config.deviceId + "/messages/events/";
this._topic_subscribe = "devices/" + config.deviceId + "/messages/devicebound/#";
debug('topic publish: ' + this._topic_publish);
@ -68,7 +68,7 @@ MqttBase.prototype.connect = function (config, done) {
this._options.passphrase = config.x509.passphrase;
}
this.client = this.mqttprovider.connect(this._hostName, this._options);
this.client = this.mqttprovider.connect(uri, this._options);
/*Codes_SRS_NODE_COMMON_MQTT_BASE_16_007: [The `connect` method shall not throw if the `done` argument has not been passed.]*/
if (done) {
var errCallback = function (error) {

11
device/transport/mqtt/lib/mqtt_ws.d.ts поставляемый Normal file
Просмотреть файл

@ -0,0 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { Client } from 'azure-iot-device';
import Mqtt = require('./mqtt');
declare class MqttWs extends Mqtt implements Client.Transport {
constructor(config: Client.Config);
}
export = MqttWs;

Просмотреть файл

@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var Base = require('./mqtt.js');
var util = require('util');
/**
* @class module:azure-iot-device-mqtt.MqttWs
* @classdesc Provides MQTT over WebSockets transport for the device [client]{@link module:azure-iot-device.Client} class.
*
* @param {Object} config Configuration object derived from the connection string by the client.
*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_001: [The constructor shall accept the transport configuration structure.]*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_002: [The constructor shall store the configuration structure in a member variable.]*/
/*Codes_SRS_NODE_DEVICE_MQTT_12_003: [The constructor shall create an base transport object and store it in a member variable.]*/
function MqttWs(config) {
Base.call(this, config);
/*Codes_SRS_NODE_DEVICE_MQTT_16_017: [The `MqttWs` constructor shall initialize the `uri` property of the `config` object to `wss://<host>:443/$iothub/websocket`.]*/
this._config.uri = 'wss://' + config.host + ':443/$iothub/websocket';
}
util.inherits(MqttWs, Base);
module.exports = MqttWs;

Просмотреть файл

@ -29,7 +29,7 @@
"alltest": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter spec test/_*_test*.js",
"ci": "npm -s run lint && npm -s run alltest-min && npm -s run check-cover",
"test": "npm -s run lint && npm -s run unittest",
"check-cover": "istanbul check-coverage --statements 79 --branches 72 --functions 48 --lines 81"
"check-cover": "istanbul check-coverage --statements 80 --branches 73 --functions 50 --lines 82"
},
"engines": {
"node": ">= 0.10"

Просмотреть файл

@ -11,9 +11,6 @@ var FakeMqtt = require('./_fake_mqtt.js');
var Message = require('azure-iot-common').Message;
describe('MqttBase', function () {
describe('#constructor', function () {
});
describe('#connect', function() {
/*Tests_SRS_NODE_COMMON_MQTT_BASE_16_006: [The `connect` method shall throw a ReferenceError if the config argument is falsy, or if one of the following properties of the config argument is falsy: deviceId, host, and one of sharedAccessSignature or x509.cert and x509.key.]*/
it('throws if config structure is falsy', function () {
@ -136,6 +133,41 @@ describe('MqttBase', function () {
transport.connect(config);
});
it('uses mqtts as a protocol by default', function () {
var config = {
host: "host.name",
deviceId: "deviceId",
sharedAccessSignature: "sasToken"
};
var fakemqtt = new FakeMqtt();
var transport = new MqttBase(fakemqtt);
fakemqtt.connect = function(host) {
assert.strictEqual(host, 'mqtts://' + config.host);
};
transport.connect(config);
});
it('uses the uri specified by the config object', function () {
var config = {
host: "host.name",
deviceId: "deviceId",
sharedAccessSignature: "sasToken",
uri: 'wss://host.name:443/$iothub/websocket'
};
var fakemqtt = new FakeMqtt();
var transport = new MqttBase(fakemqtt);
fakemqtt.connect = function(host) {
assert.strictEqual(host, config.uri);
};
transport.connect(config);
});
/*Tests_SRS_NODE_COMMON_MQTT_BASE_12_005: [The `connect` method shall call connect on MQTT.JS library and call the `done` callback with a `null` error object and the result as a second argument.]*/
it('calls the done callback once successfully connected to the server', function(done) {
var config = {

Просмотреть файл

@ -23,6 +23,12 @@ describe('Mqtt', function () {
assert.notEqual(mqtt, undefined);
assert.equal(mqtt._config, fakeConfig);
});
/*Tests_SRS_NODE_DEVICE_MQTT_16_016: [The `Mqtt` constructor shall initialize the `uri` property of the `config` object to `mqtts://<host>`.]*/
it('sets the uri property to \'mqtts://<host>\'', function () {
var mqtt = new Mqtt(fakeConfig);
assert.strictEqual(mqtt._config.uri, 'mqtts://' + fakeConfig.host);
});
});
describe('#setOptions', function() {

Просмотреть файл

@ -0,0 +1,31 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var assert = require('chai').assert;
var MqttWs = require('../lib/mqtt_ws.js');
describe('MqttWs', function () {
var fakeConfig = {
host: 'host.name',
deviceId: 'deviceId'
};
describe('#constructor', function () {
/* Tests_SRS_NODE_DEVICE_MQTT_12_001: [The `Mqtt` constructor shall accept the transport configuration structure */
/* Tests_SRS_NODE_DEVICE_MQTT_12_002: [The `Mqtt` constructor shall store the configuration structure in a member variable */
it('stores config and created transport in member', function () {
var mqttWs = new MqttWs(fakeConfig);
assert.notEqual(mqttWs, null);
assert.notEqual(mqttWs, undefined);
assert.equal(mqttWs._config, fakeConfig);
});
/*Tests_SRS_NODE_DEVICE_MQTT_16_017: [The `MqttWs` constructor shall initialize the `uri` property of the `config` object to `wss://<host>:443/$iothub/websocket`.]*/
it('sets the uri property to \'wss://<host>:443/$iothub/websocket\'', function () {
var mqttWs = new MqttWs(fakeConfig);
assert.equal(mqttWs._config.uri, 'wss://' + fakeConfig.host + ':443/$iothub/websocket');
});
});
});