Skip to content

Commit

Permalink
Implements offline event #111.
Browse files Browse the repository at this point in the history
  • Loading branch information
sclausen committed Sep 8, 2018
1 parent 5969b37 commit f7965c7
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/mqtt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class MqttService {

private _onConnect: EventEmitter<IOnConnectEvent> = new EventEmitter<IOnConnectEvent>();
private _onClose: EventEmitter<void> = new EventEmitter<void>();
private _onOffline: EventEmitter<void> = new EventEmitter<void>();
private _onError: EventEmitter<IOnErrorEvent> = new EventEmitter<IOnErrorEvent>();
private _onReconnect: EventEmitter<void> = new EventEmitter<void>();
private _onMessage: EventEmitter<IOnMessageEvent> = new EventEmitter<IOnMessageEvent>();
Expand Down Expand Up @@ -115,6 +116,7 @@ export class MqttService {
this.client.stream.on('error', this._handleOnError);
this.client.on('reconnect', this._handleOnReconnect);
this.client.on('message', this._handleOnMessage);
this.client.on('offline', this._handleOnOffline);
}

/**
Expand Down Expand Up @@ -285,6 +287,11 @@ export class MqttService {
return this._onClose;
}

/** An EventEmitter to listen to offline events */
public get onOffline(): EventEmitter<void> {
return this._onOffline;
}

/** An EventEmitter to listen to connect messages */
public get onConnect(): EventEmitter<IOnConnectEvent> {
return this._onConnect;
Expand Down Expand Up @@ -315,6 +322,11 @@ export class MqttService {
this._onClose.emit();
}

private _handleOnOffline = () => {
this.state.next(MqttConnectionState.CLOSED);
this._onOffline.emit();
}

private _handleOnConnect = (e: IOnConnectEvent) => {
if (this.options.connectOnCreate === true) {
Object.keys(this.observables).forEach((filter: string) => {
Expand Down

0 comments on commit f7965c7

Please sign in to comment.