Skip to content

Commit

Permalink
[FEATURE] Allows to set a connection url instead of the parametrized …
Browse files Browse the repository at this point in the history
…url parts. do not fill not given port and path with standard values. Resolves #143.
  • Loading branch information
sclausen committed Oct 11, 2019
1 parent 6d5015e commit 0f27388
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package-lock.json
*.metadata.json
.vscode
coverage
documentation
bundles
node_modules
npm-debug.log
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-mqtt",
"version": "6.11.0",
"version": "6.13.0",
"description": "ngx mqtt client library",
"main": "bundles/ngx-mqtt.min.js",
"module": "./src/index.js",
Expand All @@ -9,8 +9,6 @@
"clean": "node clean",
"test": "karma start",
"build": "node clean && ngc && webpack",
"docs": "compodoc src -p tsconfig.json",
"serve:docs": "http-server documentation",
"prepare": "node clean && ngc && webpack"
},
"repository": {
Expand Down Expand Up @@ -50,7 +48,6 @@
"@angular/core": ">=6.0.4",
"@angular/platform-browser": ">=6.0.4",
"@angular/platform-browser-dynamic": ">=6.0.4",
"@compodoc/compodoc": "^1.1.3",
"@types/fs-extra": "^5.0.3",
"@types/hammerjs": "2.0.33",
"@types/jasmine": "2.5.35",
Expand Down
4 changes: 3 additions & 1 deletion src/mqtt.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface IMqttServiceOptions extends IClientOptions {
/** the path parameters to connect to e.g. `/mqtt` */
path?: string;
protocol?: 'wss' | 'ws';
/** if the url is provided, hostname, port path and protocol are ignored */
url?: string;
}

export interface IMqttMessage extends IPacket {
Expand Down Expand Up @@ -49,4 +51,4 @@ export interface IMqttClient extends MqttClient {
}

export interface IOnPacketsendEvent extends IPacket { }
export interface IOnPacketreceiveEvent extends IPacket { }
export interface IOnPacketreceiveEvent extends IPacket { }
33 changes: 20 additions & 13 deletions src/mqtt.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter, Inject, Injectable } from '@angular/core';
import { ISubscriptionGrant, IClientSubscribeOptions } from 'mqtt';
import { connect } from '../vendor/mqtt.min.js';
import {EventEmitter, Inject, Injectable} from '@angular/core';
import {ISubscriptionGrant, IClientSubscribeOptions} from 'mqtt';
import {connect} from '../vendor/mqtt.min.js';
import * as extend from 'xtend';

import {
Expand Down Expand Up @@ -35,7 +35,7 @@ import {
MqttConnectionState
} from './mqtt.model';

import { MqttServiceConfig, MqttClientService } from './mqtt.module';
import {MqttServiceConfig, MqttClientService} from './mqtt.module';

/**
* With an instance of MqttService, you can observe and subscribe to MQTT in multiple places, e.g. in different components,
Expand Down Expand Up @@ -92,9 +92,13 @@ export class MqttService {
const options = extend(this.options || {}, opts);
const protocol = options.protocol || 'ws';
const hostname = options.hostname || 'localhost';
const port = options.port || 1884;
const path = options.path || '/';
this._url = `${protocol}://${hostname}:${port}/${path}`;
if (options.url) {
this._url = options.url;
} else {
this._url = `${protocol}://${hostname}`;
this._url += options.port ? `:${options.port}` : '';
this._url += options.path ? `${options.path}` : '';
}
this.state.next(MqttConnectionState.CONNECTING);
const mergedOptions = extend({
clientId: this._clientId,
Expand Down Expand Up @@ -150,7 +154,7 @@ export class MqttService {
* The last one unsubscribing this filter executes a mqtt unsubscribe.
* Every new subscriber gets the latest message.
*/
public observeRetained(filterString: string, opts: IClientSubscribeOptions = { qos: 1 }): Observable<IMqttMessage> {
public observeRetained(filterString: string, opts: IClientSubscribeOptions = {qos: 1}): Observable<IMqttMessage> {
return this._generalObserve(filterString, () => publishReplay(1), opts);
}

Expand All @@ -160,7 +164,7 @@ export class MqttService {
* The first one subscribing to the resulting observable executes a mqtt subscribe.
* The last one unsubscribing this filter executes a mqtt unsubscribe.
*/
public observe(filterString: string, opts: IClientSubscribeOptions = { qos: 1 }): Observable<IMqttMessage> {
public observe(filterString: string, opts: IClientSubscribeOptions = {qos: 1}): Observable<IMqttMessage> {
return this._generalObserve(filterString, () => publish(), opts);
}

Expand Down Expand Up @@ -191,7 +195,7 @@ export class MqttService {
this.client.unsubscribe(granted_.topic);
rejected.error(`subscription for '${granted_.topic}' rejected!`);
}
this._onSuback.emit({ filter: filterString, granted: granted_.qos !== 128 });
this._onSuback.emit({filter: filterString, granted: granted_.qos !== 128});
});
}
});
Expand Down Expand Up @@ -276,13 +280,16 @@ export class MqttService {
switch (f) {
// In case the filter level is '#', this is a match no matter whether
// the topic is undefined on this level or not ('#' matches parent element as well!).
case '#': return true;
case '#':
return true;
// In case the filter level is '+', we shall dive into the recursion only if t is not undefined.
case '+': return t ? match() : false;
case '+':
return t ? match() : false;
// In all other cases the filter level must match the topic level,
// both must be defined and the filter tail must match the topic
// tail (which is determined by the recursive call of match()).
default: return f === t && (f === undefined ? true : match());
default:
return f === t && (f === undefined ? true : match());
}
};
return match();
Expand Down
1 change: 0 additions & 1 deletion vendor/mqtt.min.js

This file was deleted.

0 comments on commit 0f27388

Please sign in to comment.