-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmqtt5.ts
More file actions
848 lines (726 loc) · 31.8 KB
/
mqtt5.ts
File metadata and controls
848 lines (726 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
/**
* Browser specific MQTT5 client implementation
*
* @packageDocumentation
* @module mqtt5
* @mergeTarget
*
*/
import {BufferedEventEmitter} from "../common/event";
import * as mqtt from "mqtt"; /* The mqtt-js external dependency */
import * as mqtt5 from "../common/mqtt5";
import {OutboundTopicAliasBehaviorType} from "../common/mqtt5";
import * as mqtt5_packet from "../common/mqtt5_packet"
import {CrtError} from "./error";
import * as WebsocketUtils from "./ws";
import * as mqtt_utils from "./mqtt5_utils";
import * as mqtt_shared from "../common/mqtt_shared";
import * as auth from "./auth";
export * from "../common/mqtt5";
export * from '../common/mqtt5_packet';
export { Mqtt5ClientConfigBase } from "../common/mqtt_shared";
/**
* Factory function that allows the user to completely control the url used to form the websocket handshake
* request.
*/
export type Mqtt5WebsocketUrlFactory = () => string;
/**
* Type of url to construct when establishing an MQTT5 connection over websockets
*/
export enum Mqtt5WebsocketUrlFactoryType {
/**
* Websocket connection over plain-text with no additional handshake transformation
*/
Ws = 1,
/**
* Websocket connection over TLS with no additional handshake transformation
*/
Wss = 2,
/**
* Websocket connection over TLS with a handshake signed by the Aws Sigv4 signing process
*/
Sigv4 = 3,
/**
* Websocket connection whose url is formed by a user-supplied callback function
*/
Custom = 4
}
/**
* Websocket factory options discriminated union variant for untransformed connections over plain-text
*/
export interface Mqtt5WebsocketUrlFactoryWsOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Ws;
};
/**
* Websocket factory options discriminated union variant for untransformed connections over TLS
*/
export interface Mqtt5WebsocketUrlFactoryWssOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Wss;
};
/**
* Websocket factory options discriminated union variant for untransformed connections over TLS signed by
* the AWS Sigv4 signing process.
*/
export interface Mqtt5WebsocketUrlFactorySigv4Options {
urlFactory : Mqtt5WebsocketUrlFactoryType.Sigv4;
/**
* AWS Region to sign against.
*/
region?: string;
/**
* Provider to source AWS credentials from
*/
credentialsProvider: auth.CredentialsProvider;
}
/**
* Websocket factory options discriminated union variant for arbitrarily transformed handshake urls.
*/
export interface Mqtt5WebsocketUrlFactoryCustomOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Custom;
customUrlFactory: Mqtt5WebsocketUrlFactory;
};
/**
* Union of all websocket factory option possibilities.
*/
export type Mqtt5WebsocketUrlFactoryOptions = Mqtt5WebsocketUrlFactoryWsOptions | Mqtt5WebsocketUrlFactoryWssOptions | Mqtt5WebsocketUrlFactorySigv4Options | Mqtt5WebsocketUrlFactoryCustomOptions;
/**
* Browser-specific websocket configuration options for connection establishment
*/
export interface Mqtt5WebsocketConfig {
/**
* Options determining how the websocket url is created.
*/
urlFactoryOptions : Mqtt5WebsocketUrlFactoryOptions;
/**
* Opaque options set passed through to the underlying websocket implementation regardless of url factory.
* Use this to control proxy settings amongst other things.
*/
wsOptions?: any;
}
/**
* Configuration options for mqtt5 client creation.
*/
export interface Mqtt5ClientConfig extends mqtt_shared.Mqtt5ClientConfigBase {
/**
* Overall time interval to wait to establish an MQTT connection. If a complete MQTT connection (from socket
* establishment all the way up to CONNACK receipt) has not been established before this timeout expires,
* the connection attempt will be considered a failure.
*
* @group Browser-only
*/
connectTimeoutMs? : number;
/**
* Options for the underlying websocket connection
*
* @group Browser-only
*/
websocketOptions?: Mqtt5WebsocketConfig;
}
/**
* @internal
*
* Mqtt-js only supports reconnect on a fixed delay.
*
* This helper class allows for variable time-delay rescheduling of reconnect attempts by implementing the
* reconnect delay options supported by the native client. Variable-delay reconnect actually happens by configuring
* the mqtt-js client to have a much longer reconnect delay than our configured maximum and then letting this class
* "interrupt" that long reconnect delay with the real, shorter wait-then-connect each time.
*/
class ReconnectionScheduler {
private connectionFailureCount: number;
private lastReconnectDelay: number | undefined;
private resetConnectionFailureCountTask : ReturnType<typeof setTimeout> | undefined;
private reconnectionTask : ReturnType<typeof setTimeout> | undefined;
constructor(private browserClient: mqtt.MqttClient, private clientConfig: Mqtt5ClientConfig) {
this.connectionFailureCount = 0;
this.lastReconnectDelay = 0;
this.resetConnectionFailureCountTask = undefined;
this.reconnectionTask = undefined;
this.lastReconnectDelay = undefined;
}
/**
* Invoked by the client when a successful connection is established. Schedules the task that will reset the
* delay if a configurable amount of time elapses with a good connection.
*/
onSuccessfulConnection() : void {
this.clearTasks();
this.resetConnectionFailureCountTask = setTimeout(() => {
this.connectionFailureCount = 0;
this.lastReconnectDelay = undefined;
}, this.clientConfig.minConnectedTimeToResetReconnectDelayMs ?? mqtt_utils.DEFAULT_MIN_CONNECTED_TIME_TO_RESET_RECONNECT_DELAY_MS);
}
/**
* Invoked by the client after a disconnection or connection failure occurs. Schedules the next reconnect
* task.
*/
onConnectionFailureOrDisconnection() : void {
this.clearTasks();
let nextDelay : number = this.calculateNextReconnectDelay();
this.lastReconnectDelay = nextDelay;
this.connectionFailureCount += 1;
this.reconnectionTask = setTimeout(async () => {
let wsOptions = this.clientConfig.websocketOptions;
if (wsOptions && wsOptions.urlFactoryOptions.urlFactory == Mqtt5WebsocketUrlFactoryType.Sigv4) {
let sigv4Options = wsOptions.urlFactoryOptions as Mqtt5WebsocketUrlFactorySigv4Options;
if (sigv4Options.credentialsProvider) {
await sigv4Options.credentialsProvider.refreshCredentials();
}
}
this.browserClient.reconnect();
}, nextDelay);
}
/**
* Resets any reconnect/clear-delay tasks.
*/
clearTasks() : void {
if (this.reconnectionTask) {
clearTimeout(this.reconnectionTask);
}
if (this.resetConnectionFailureCountTask) {
clearTimeout(this.resetConnectionFailureCountTask);
}
}
private randomInRange(min: number, max: number) : number {
return min + (max - min) * Math.random();
}
/**
* Computes the next reconnect delay based on the Jitter/Retry configuration.
* Implements jitter calculations in https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
* @private
*/
private calculateNextReconnectDelay() : number {
const jitterType : mqtt5.RetryJitterType = this.clientConfig.retryJitterMode ?? mqtt5.RetryJitterType.Default;
const [minDelay, maxDelay] : [number, number] = mqtt_utils.getOrderedReconnectDelayBounds(this.clientConfig.minReconnectDelayMs, this.clientConfig.maxReconnectDelayMs);
const clampedFailureCount : number = Math.min(52, this.connectionFailureCount);
let delay : number = 0;
if (jitterType == mqtt5.RetryJitterType.None) {
delay = minDelay * Math.pow(2, clampedFailureCount);
} else if (jitterType == mqtt5.RetryJitterType.Decorrelated && this.lastReconnectDelay) {
delay = this.randomInRange(minDelay, 3 * this.lastReconnectDelay);
} else {
delay = this.randomInRange(minDelay, Math.min(maxDelay, minDelay * Math.pow(2, clampedFailureCount)));
}
delay = Math.min(maxDelay, delay);
this.lastReconnectDelay = delay;
return delay;
}
}
/**
* Elements of a simple state machine that allows us to adapt the mqtt-js control model to our mqtt5 client
* control model (start/stop).
*
* @internal
*/
enum Mqtt5ClientState {
Stopped = 0,
Running = 1,
Stopping = 2,
Restarting = 3,
}
/**
* Elements of a simple state machine that allows us to adapt the mqtt-js event set to our mqtt5 client's
* lifecycle event set.
*
* @internal
*/
enum Mqtt5ClientLifecycleEventState {
None = 0,
Connecting = 1,
Connected = 2,
Disconnected = 3,
}
/**
* Browser specific MQTT5 client implementation
*/
export class Mqtt5Client extends BufferedEventEmitter implements mqtt5.IMqtt5Client {
private browserClient?: mqtt.MqttClient;
private state : Mqtt5ClientState;
private lifecycleEventState : Mqtt5ClientLifecycleEventState;
private lastDisconnect? : mqtt5_packet.DisconnectPacket;
private lastError? : Error;
private reconnectionScheduler? : ReconnectionScheduler;
private mqttJsConfig : mqtt.IClientOptions;
private topicAliasBindings : Map<number, string>;
/**
* Client constructor
*
* @param config The configuration for this client
*/
constructor(private config: Mqtt5ClientConfig) {
super();
this.mqttJsConfig = mqtt_utils.create_mqtt_js_client_config_from_crt_client_config(this.config);
this.state = Mqtt5ClientState.Stopped;
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.None;
this.topicAliasBindings = new Map<number, string>();
}
/**
* Triggers cleanup of native resources associated with the MQTT5 client. On the browser, the implementation is
* an empty function.
*/
close() {}
/**
* Notifies the MQTT5 client that you want it to maintain connectivity to the configured endpoint.
* The client will attempt to stay connected using the properties of the reconnect-related parameters
* in the mqtt5 client configuration.
*
* This is an asynchronous operation.
*/
start() {
if (this.state == Mqtt5ClientState.Stopped) {
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.Connecting;
this.lastDisconnect = undefined;
/* pause event emission until everything is fully-initialized */
this.cork();
this.emit('attemptingConnect');
const create_websocket_stream = (client: mqtt.MqttClient) => WebsocketUtils.create_mqtt5_websocket_stream(this.config);
this.browserClient = new mqtt.MqttClient(create_websocket_stream, this.mqttJsConfig);
// hook up events
this.browserClient.on('end', () => {this._on_stopped_internal();});
this.browserClient.on('reconnect', () => {this.on_attempting_connect();});
this.browserClient.on('connect', (connack: mqtt.IConnackPacket) => {this.on_connection_success(connack);});
this.browserClient.on('message', (topic: string, payload: Buffer, packet: mqtt.IPublishPacket) => { this.on_message(topic, payload, packet);});
this.browserClient.on('error', (error: Error) => { this.on_browser_client_error(error); });
this.browserClient.on('close', () => { this.on_browser_close(); });
this.browserClient.on('disconnect', (packet: mqtt.IDisconnectPacket) => { this.on_browser_disconnect_packet(packet); });
this.reconnectionScheduler = new ReconnectionScheduler(this.browserClient, this.config);
this.state = Mqtt5ClientState.Running;
/* unpause event emission */
this.uncork();
} else if (this.state == Mqtt5ClientState.Stopping) {
this.state = Mqtt5ClientState.Restarting;
}
}
/**
* Notifies the MQTT5 client that you want it to end connectivity to the configured endpoint, disconnecting any
* existing connection and halting reconnection attempts.
*
* This is an asynchronous operation. Once the process completes, no further events will be emitted until the client
* has {@link start} invoked. Invoking {@link start start()} after a {@link stop stop()} will always result in
* a new MQTT session.
*
* @param disconnectPacket (optional) properties of a DISCONNECT packet to send as part of the shutdown process
*/
stop(disconnectPacket?: mqtt5_packet.DisconnectPacket) {
if (this.state == Mqtt5ClientState.Running) {
if (disconnectPacket) {
this.browserClient?.end(true, mqtt_utils.transform_crt_disconnect_to_mqtt_js_disconnect(disconnectPacket));
} else {
this.browserClient?.end(true);
}
this.state = Mqtt5ClientState.Stopping;
} else if (this.state == Mqtt5ClientState.Restarting) {
this.state = Mqtt5ClientState.Stopping;
}
}
/**
* Subscribe to one or more topic filters by queuing a SUBSCRIBE packet to be sent to the server.
*
* @param packet SUBSCRIBE packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the SUBACK response
*/
async subscribe(packet: mqtt5_packet.SubscribePacket) : Promise<mqtt5_packet.SubackPacket> {
return new Promise<mqtt5_packet.SubackPacket>((resolve, reject) => {
try {
if (!this.browserClient) {
reject(new Error("Client is stopped and cannot subscribe"));
return;
}
if (!packet) {
reject(new Error("Invalid subscribe packet"));
return;
}
let subMap: mqtt.ISubscriptionMap = mqtt_utils.transform_crt_subscribe_to_mqtt_js_subscription_map(packet);
let subOptions: mqtt.IClientSubscribeOptions = mqtt_utils.transform_crt_subscribe_to_mqtt_js_subscribe_options(packet);
// @ts-ignore
this.browserClient.subscribe(subMap, subOptions, (error, grants) => {
if (error) {
reject(error);
return;
}
const suback: mqtt5_packet.SubackPacket = mqtt_utils.transform_mqtt_js_subscription_grants_to_crt_suback(grants);
resolve(suback);
});
} catch (err) {
reject(err);
}
});
}
/**
* Unsubscribe from one or more topic filters by queuing an UNSUBSCRIBE packet to be sent to the server.
*
* @param packet UNSUBSCRIBE packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the UNSUBACK response
*/
async unsubscribe(packet: mqtt5_packet.UnsubscribePacket) : Promise<mqtt5_packet.UnsubackPacket> {
return new Promise<mqtt5_packet.UnsubackPacket>((resolve, reject) => {
try {
if (!this.browserClient) {
reject(new Error("Client is stopped and cannot unsubscribe"));
return;
}
if (!packet) {
reject(new Error("Invalid unsubscribe packet"));
return;
}
let topicFilters: string[] = packet.topicFilters;
let unsubOptions: Object = mqtt_utils.transform_crt_unsubscribe_to_mqtt_js_unsubscribe_options(packet);
this.browserClient.unsubscribe(topicFilters, unsubOptions, (error, packet) => {
if (error) {
reject(error);
return;
}
/*
* sigh, mqtt-js doesn't emit the unsuback packet, we have to make something up that won't reflect
* reality.
*/
if (!packet || packet.cmd !== 'unsuback') {
/* this is a complete lie */
let unsuback: mqtt5_packet.UnsubackPacket = {
type: mqtt5_packet.PacketType.Unsuback,
reasonCodes: topicFilters.map((filter: string, index: number, array: string[]): mqtt5_packet.UnsubackReasonCode => {
return mqtt5_packet.UnsubackReasonCode.Success;
})
};
resolve(unsuback);
} else {
const unsuback: mqtt5_packet.UnsubackPacket = mqtt_utils.transform_mqtt_js_unsuback_to_crt_unsuback(packet as mqtt.IUnsubackPacket);
resolve(unsuback);
}
});
} catch (err) {
reject(err);
}
});
}
private reset_topic_aliases() {
this.topicAliasBindings.clear();
}
private bind_topic_alias(alias: number, topic: string) {
this.topicAliasBindings.set(alias, topic);
}
private is_topic_alias_bound(alias: number, topic: string) {
if (!topic) {
return false;
}
return this.topicAliasBindings.get(alias) === topic;
}
/**
* Send a message to subscribing clients by queuing a PUBLISH packet to be sent to the server.
*
* @param packet PUBLISH packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the PUBACK response (QoS 1), or
* undefined (QoS 0)
*/
async publish(packet: mqtt5_packet.PublishPacket) : Promise<mqtt5.PublishCompletionResult> {
return new Promise<mqtt5.PublishCompletionResult>((resolve, reject) => {
try {
if (!this.browserClient) {
reject(new Error("Client is stopped and cannot publish"));
return;
}
if (!packet) {
reject(new Error("Invalid publish packet"));
return;
}
/*
* Out topic aliasing contract and mqtt-js's don't quite match, so we do some fixup here.
*
* In our manual mode, the contract is that you must *always* submit both the topic and the alias.
*
* In Mqtt-js's manual mode, the alias will only be used if it's been previously bound and you don't
* submit an alias in the publish (this is not reasonable behavior, but it's not under out control).
* So when we're in manual aliasing mode, we track all the current bindings and strip out the alias
* when there's a match, signaling to mqtt-js that the alias binding should be used.
*/
if ((this.config.topicAliasingOptions?.outboundBehavior ?? OutboundTopicAliasBehaviorType.Default) == OutboundTopicAliasBehaviorType.Manual) {
if (packet.topicAlias && this.lifecycleEventState == Mqtt5ClientLifecycleEventState.Connected) {
if (this.is_topic_alias_bound(packet.topicAlias, packet.topicName)) {
delete (packet.topicAlias);
}
this.bind_topic_alias(packet.topicAlias, packet.topicName);
}
} else {
delete (packet.topicAlias);
}
let publishOptions : mqtt.IClientPublishOptions = mqtt_utils.transform_crt_publish_to_mqtt_js_publish_options(packet);
let qos : mqtt5_packet.QoS = packet.qos;
let payload = mqtt_shared.normalize_payload(packet.payload);
this.browserClient.publish(packet.topicName, payload, publishOptions, (error, completionPacket) => {
if (error) {
reject(error);
return;
}
switch (qos) {
case mqtt5_packet.QoS.AtMostOnce:
resolve(undefined);
break;
case mqtt5_packet.QoS.AtLeastOnce:
if (!completionPacket) {
reject(new Error("Invalid puback packet from mqtt-js"));
return;
}
/*
* sadly, mqtt-js returns the original publish packet when the puback is a success, so we have
* to create a fake puback instead. This means we won't reflect any reason string or
* user properties that might have been present in the real puback.
*/
if (completionPacket.cmd !== "puback") {
resolve({
type: mqtt5_packet.PacketType.Puback,
reasonCode: mqtt5_packet.PubackReasonCode.Success
})
}
const puback: mqtt5_packet.PubackPacket = mqtt_utils.transform_mqtt_js_puback_to_crt_puback(completionPacket as mqtt.IPubackPacket);
resolve(puback);
break;
default:
/* Technically, mqtt-js supports QoS 2 but we don't yet model it in the CRT types */
reject(new Error("Unsupported QoS value"));
break;
}
});
} catch (err) {
reject(err);
}
});
}
/**
* Queries whether the client is currently connected
*
* @returns whether the client is currently connected
*/
isConnected() : boolean {
return this.lifecycleEventState == Mqtt5ClientLifecycleEventState.Connected;
}
/**
* Event emitted when the client encounters a disruptive error condition. Not currently used.
*
* Listener type: {@link ErrorEventListener}
*
* @event
*/
static ERROR : string = 'error';
/**
* Event emitted when the client encounters a transient error event that will not disrupt promises based on
* lifecycle events. Currently, mqtt-js client error events are relayed to this event.
*
* Listener type: {@link ErrorEventListener}
*
* @event
* @group Browser-only
*/
static INFO : string = 'info';
/**
* Event emitted when an MQTT PUBLISH packet is received by the client.
*
* Listener type: {@link MessageReceivedEventListener}
*
* @event
*/
static MESSAGE_RECEIVED : string = 'messageReceived';
/**
* Event emitted when the client begins a connection attempt.
*
* Listener type: {@link AttemptingConnectEventListener}
*
* @event
*/
static ATTEMPTING_CONNECT : string = 'attemptingConnect';
/**
* Event emitted when the client successfully establishes an MQTT connection. Only emitted after
* an {@link ATTEMPTING_CONNECT attemptingConnect} event.
*
* Listener type: {@link ConnectionSuccessEventListener}
*
* @event
*/
static CONNECTION_SUCCESS : string = 'connectionSuccess';
/**
* Event emitted when the client fails to establish an MQTT connection. Only emitted after
* an {@link ATTEMPTING_CONNECT attemptingConnect} event.
*
* Listener type: {@link ConnectionFailureEventListener}
*
* @event
*/
static CONNECTION_FAILURE : string = 'connectionFailure';
/**
* Event emitted when the client's current connection is closed for any reason. Only emitted after
* a {@link CONNECTION_SUCCESS connectionSuccess} event.
*
* Listener type: {@link DisconnectionEventListener}
*
* @event
*/
static DISCONNECTION : string = 'disconnection';
/**
* Event emitted when the client finishes shutdown as a result of the user invoking {@link stop}.
*
* Listener type: {@link StoppedEventListener}
*
* @event
*/
static STOPPED : string = 'stopped';
/**
* Registers a listener for the client's {@link ERROR error} event. An {@link ERROR error} event is emitted when
* the client encounters a disruptive error condition.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'error', listener: mqtt5.ErrorEventListener): this;
/**
* Registers a listener for the client's {@link INFO info} event. An {@link INFO info} event is emitted when
* the client encounters a transient error event that will not disrupt promises based on lifecycle events.
* Currently, mqtt-js client error events are relayed to this event.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*
* @group Browser-only
*/
on(event: 'info', listener: mqtt5.ErrorEventListener): this;
/**
* Registers a listener for the client's {@link MESSAGE_RECEIVED messageReceived} event. A
* {@link MESSAGE_RECEIVED messageReceived} event is emitted when an MQTT PUBLISH packet is received by the
* client.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'messageReceived', listener: mqtt5.MessageReceivedEventListener): this;
/**
* Registers a listener for the client's {@link ATTEMPTING_CONNECT attemptingConnect} event. A
* {@link ATTEMPTING_CONNECT attemptingConnect} event is emitted every time the client begins a connection attempt.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'attemptingConnect', listener: mqtt5.AttemptingConnectEventListener): this;
/**
* Registers a listener for the client's {@link CONNECTION_SUCCESS connectionSuccess} event. A
* {@link CONNECTION_SUCCESS connectionSuccess} event is emitted every time the client successfully establishes
* an MQTT connection.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'connectionSuccess', listener: mqtt5.ConnectionSuccessEventListener): this;
/**
* Registers a listener for the client's {@link CONNECTION_FAILURE connectionFailure} event. A
* {@link CONNECTION_FAILURE connectionFailure} event is emitted every time the client fails to establish an
* MQTT connection.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'connectionFailure', listener: mqtt5.ConnectionFailureEventListener): this;
/**
* Registers a listener for the client's {@link DISCONNECTION disconnection} event. A
* {@link DISCONNECTION disconnection} event is emitted when the client's current MQTT connection is closed
* for any reason.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'disconnection', listener: mqtt5.DisconnectionEventListener): this;
/**
* Registers a listener for the client's {@link STOPPED stopped} event. A
* {@link STOPPED stopped} event is emitted when the client finishes shutdown as a
* result of the user invoking {@link stop}.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'stopped', listener: mqtt5.StoppedEventListener): this;
on(event: string | symbol, listener: (...args: any[]) => void): this {
super.on(event, listener);
return this;
}
private on_browser_disconnect_packet(packet: mqtt.IDisconnectPacket) {
this.lastDisconnect = mqtt_utils.transform_mqtt_js_disconnect_to_crt_disconnect(packet);
}
private on_browser_close() {
let lastDisconnect : mqtt5_packet.DisconnectPacket | undefined = this.lastDisconnect;
let lastError : Error | undefined = this.lastError;
if (this.lifecycleEventState == Mqtt5ClientLifecycleEventState.Connected) {
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.Disconnected;
this.reconnectionScheduler?.onConnectionFailureOrDisconnection();
let disconnectionEvent : mqtt5.DisconnectionEvent = {
error: new CrtError(lastError?.toString() ?? "disconnected")
}
if (lastDisconnect !== undefined) {
disconnectionEvent.disconnect = lastDisconnect;
}
setTimeout(() => {
this.emit(Mqtt5Client.DISCONNECTION, disconnectionEvent);
}, 0);
} else if (this.lifecycleEventState == Mqtt5ClientLifecycleEventState.Connecting) {
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.Disconnected;
this.reconnectionScheduler?.onConnectionFailureOrDisconnection();
let connectionFailureEvent: mqtt5.ConnectionFailureEvent = {
error: new CrtError(lastError?.toString() ?? "connectionFailure")
};
setTimeout(() => {
this.emit(Mqtt5Client.CONNECTION_FAILURE, connectionFailureEvent);
}, 0);
}
this.lastDisconnect = undefined;
this.lastError = undefined;
}
private on_browser_client_error(error: Error) {
this.lastError = error;
setTimeout(() => {
this.emit(Mqtt5Client.INFO, new CrtError(error));
}, 0);
}
private on_attempting_connect () {
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.Connecting;
let attemptingConnectEvent: mqtt5.AttemptingConnectEvent = {};
setTimeout(() => {
this.emit(Mqtt5Client.ATTEMPTING_CONNECT, attemptingConnectEvent);
}, 0);
}
private on_connection_success (connack: mqtt.IConnackPacket) {
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.Connected;
this.reset_topic_aliases();
this.reconnectionScheduler?.onSuccessfulConnection();
let crt_connack : mqtt5_packet.ConnackPacket = mqtt_utils.transform_mqtt_js_connack_to_crt_connack(connack);
let settings : mqtt5.NegotiatedSettings = mqtt_utils.create_negotiated_settings(this.config, crt_connack);
let connectionSuccessEvent: mqtt5.ConnectionSuccessEvent = {
connack: crt_connack,
settings: settings
};
setTimeout(() => {
this.emit(Mqtt5Client.CONNECTION_SUCCESS, connectionSuccessEvent);
}, 0);
}
private _on_stopped_internal() {
this.reconnectionScheduler?.clearTasks();
this.reconnectionScheduler = undefined;
this.browserClient = undefined;
this.lifecycleEventState = Mqtt5ClientLifecycleEventState.None;
this.lastDisconnect = undefined;
this.lastError = undefined;
if (this.state == Mqtt5ClientState.Restarting) {
this.state = Mqtt5ClientState.Stopped;
this.start();
} else if (this.state != Mqtt5ClientState.Stopped) {
this.state = Mqtt5ClientState.Stopped;
this.emit(Mqtt5Client.STOPPED);
}
}
private on_message = (topic: string, payload: Buffer, packet: mqtt.IPublishPacket) => {
let crtPublish : mqtt5_packet.PublishPacket = mqtt_utils.transform_mqtt_js_publish_to_crt_publish(packet);
let messageReceivedEvent: mqtt5.MessageReceivedEvent = {
message: crtPublish
};
setTimeout(() => {
this.emit(Mqtt5Client.MESSAGE_RECEIVED, messageReceivedEvent);
}, 0);
}
}