-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttws31.js
2081 lines (1828 loc) · 76.4 KB
/
mqttws31.js
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Andrew Banks - initial API and implementation and initial documentation
*******************************************************************************/
// Only expose a single object name in the global namespace.
// Everything must go through this module. Global Paho.MQTT module
// only has a single public function, client, which returns
// a Paho.MQTT client object given connection details.
/**
* Send and receive messages using web browsers.
* <p>
* This programming interface lets a JavaScript client application use the MQTT V3.1 or
* V3.1.1 protocol to connect to an MQTT-supporting messaging server.
*
* The function supported includes:
* <ol>
* <li>Connecting to and disconnecting from a server. The server is identified by its host name and port number.
* <li>Specifying options that relate to the communications link with the server,
* for example the frequency of keep-alive heartbeats, and whether SSL/TLS is required.
* <li>Subscribing to and receiving messages from MQTT Topics.
* <li>Publishing messages to MQTT Topics.
* </ol>
* <p>
* The API consists of two main objects:
* <dl>
* <dt><b>{@link Paho.MQTT.Client}</b></dt>
* <dd>This contains methods that provide the functionality of the API,
* including provision of callbacks that notify the application when a message
* arrives from or is delivered to the messaging server,
* or when the status of its connection to the messaging server changes.</dd>
* <dt><b>{@link Paho.MQTT.Message}</b></dt>
* <dd>This encapsulates the payload of the message along with various attributes
* associated with its delivery, in particular the destination to which it has
* been (or is about to be) sent.</dd>
* </dl>
* <p>
* The programming interface validates parameters passed to it, and will throw
* an Error containing an error message intended for developer use, if it detects
* an error with any parameter.
* <p>
* Example:
*
* <code><pre>
client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
};
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
client.disconnect();
};
* </pre></code>
* @namespace Paho.MQTT
*/
if (typeof Paho === "undefined") {
Paho = {};
}
Paho.MQTT = (function (global) {
// Private variables below, these are only visible inside the function closure
// which is used to define the module.
var version = "@VERSION@";
var buildLevel = "@BUILDLEVEL@";
/**
* Unique message type identifiers, with associated
* associated integer values.
* @private
*/
var MESSAGE_TYPE = {
CONNECT: 1,
CONNACK: 2,
PUBLISH: 3,
PUBACK: 4,
PUBREC: 5,
PUBREL: 6,
PUBCOMP: 7,
SUBSCRIBE: 8,
SUBACK: 9,
UNSUBSCRIBE: 10,
UNSUBACK: 11,
PINGREQ: 12,
PINGRESP: 13,
DISCONNECT: 14
};
// Collection of utility methods used to simplify module code
// and promote the DRY pattern.
/**
* Validate an object's parameter names to ensure they
* match a list of expected variables name for this option
* type. Used to ensure option object passed into the API don't
* contain erroneous parameters.
* @param {Object} obj - User options object
* @param {Object} keys - valid keys and types that may exist in obj.
* @throws {Error} Invalid option parameter found.
* @private
*/
var validate = function(obj, keys) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (keys.hasOwnProperty(key)) {
if (typeof obj[key] !== keys[key])
throw new Error(format(ERROR.INVALID_TYPE, [typeof obj[key], key]));
} else {
var errorStr = "Unknown property, " + key + ". Valid properties are:";
for (var key in keys)
if (keys.hasOwnProperty(key))
errorStr = errorStr+" "+key;
throw new Error(errorStr);
}
}
}
};
/**
* Return a new function which runs the user function bound
* to a fixed scope.
* @param {function} User function
* @param {object} Function scope
* @return {function} User function bound to another scope
* @private
*/
var scope = function (f, scope) {
return function () {
return f.apply(scope, arguments);
};
};
/**
* Unique message type identifiers, with associated
* associated integer values.
* @private
*/
var ERROR = {
OK: {code:0, text:"AMQJSC0000I OK."},
CONNECT_TIMEOUT: {code:1, text:"AMQJSC0001E Connect timed out."},
SUBSCRIBE_TIMEOUT: {code:2, text:"AMQJS0002E Subscribe timed out."},
UNSUBSCRIBE_TIMEOUT: {code:3, text:"AMQJS0003E Unsubscribe timed out."},
PING_TIMEOUT: {code:4, text:"AMQJS0004E Ping timed out."},
INTERNAL_ERROR: {code:5, text:"AMQJS0005E Internal error."},
CONNACK_RETURNCODE: {code:6, text:"AMQJS0006E Bad Connack return code:{0} {1}."},
SOCKET_ERROR: {code:7, text:"AMQJS0007E Socket error:{0}."},
SOCKET_CLOSE: {code:8, text:"AMQJS0008I Socket closed."},
MALFORMED_UTF: {code:9, text:"AMQJS0009E Malformed UTF data:{0} {1} {2}."},
UNSUPPORTED: {code:10, text:"AMQJS0010E {0} is not supported by this browser."},
INVALID_STATE: {code:11, text:"AMQJS0011E Invalid state {0}."},
INVALID_TYPE: {code:12, text:"AMQJS0012E Invalid type {0} for {1}."},
INVALID_ARGUMENT: {code:13, text:"AMQJS0013E Invalid argument {0} for {1}."},
UNSUPPORTED_OPERATION: {code:14, text:"AMQJS0014E Unsupported operation."},
INVALID_STORED_DATA: {code:15, text:"AMQJS0015E Invalid data in local storage key={0} value={1}."},
INVALID_MQTT_MESSAGE_TYPE: {code:16, text:"AMQJS0016E Invalid MQTT message type {0}."},
MALFORMED_UNICODE: {code:17, text:"AMQJS0017E Malformed Unicode string:{0} {1}."},
};
/** CONNACK RC Meaning. */
var CONNACK_RC = {
0:"Connection Accepted",
1:"Connection Refused: unacceptable protocol version",
2:"Connection Refused: identifier rejected",
3:"Connection Refused: server unavailable",
4:"Connection Refused: bad user name or password",
5:"Connection Refused: not authorized"
};
/**
* Format an error message text.
* @private
* @param {error} ERROR.KEY value above.
* @param {substitutions} [array] substituted into the text.
* @return the text with the substitutions made.
*/
var format = function(error, substitutions) {
var text = error.text;
if (substitutions) {
for (var i=0; i<substitutions.length; i++) {
field = "{"+i+"}";
start = text.indexOf(field);
if(start > 0) {
var part1 = text.substring(0,start);
var part2 = text.substring(start+field.length);
text = part1+substitutions[i]+part2;
}
}
}
return text;
};
//MQTT protocol and version 6 M Q I s d p 3
var MqttProtoIdentifierv3 = [0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03];
//MQTT proto/version for 311 4 M Q T T 4
var MqttProtoIdentifierv4 = [0x00,0x04,0x4d,0x51,0x54,0x54,0x04];
/**
* Construct an MQTT wire protocol message.
* @param type MQTT packet type.
* @param options optional wire message attributes.
*
* Optional properties
*
* messageIdentifier: message ID in the range [0..65535]
* payloadMessage: Application Message - PUBLISH only
* connectStrings: array of 0 or more Strings to be put into the CONNECT payload
* topics: array of strings (SUBSCRIBE, UNSUBSCRIBE)
* requestQoS: array of QoS values [0..2]
*
* "Flag" properties
* cleanSession: true if present / false if absent (CONNECT)
* willMessage: true if present / false if absent (CONNECT)
* isRetained: true if present / false if absent (CONNECT)
* userName: true if present / false if absent (CONNECT)
* password: true if present / false if absent (CONNECT)
* keepAliveInterval: integer [0..65535] (CONNECT)
*
* @private
* @ignore
*/
var WireMessage = function (type, options) {
this.type = type;
for (var name in options) {
if (options.hasOwnProperty(name)) {
this[name] = options[name];
}
}
};
WireMessage.prototype.encode = function() {
// Compute the first byte of the fixed header
var first = ((this.type & 0x0f) << 4);
/*
* Now calculate the length of the variable header + payload by adding up the lengths
* of all the component parts
*/
remLength = 0;
topicStrLength = new Array();
// if the message contains a messageIdentifier then we need two bytes for that
if (this.messageIdentifier != undefined)
remLength += 2;
switch(this.type) {
// If this a Connect then we need to include 12 bytes for its header
case MESSAGE_TYPE.CONNECT:
switch(this.mqttVersion) {
case 3:
remLength += MqttProtoIdentifierv3.length + 3;
break;
case 4:
remLength += MqttProtoIdentifierv4.length + 3;
break;
}
remLength += UTF8Length(this.clientId) + 2;
if (this.willMessage != undefined) {
remLength += UTF8Length(this.willMessage.destinationName) + 2;
// Will message is always a string, sent as UTF-8 characters with a preceding length.
var willMessagePayloadBytes = this.willMessage.payloadBytes;
if (!(willMessagePayloadBytes instanceof Uint8Array))
willMessagePayloadBytes = new Uint8Array(payloadBytes);
remLength += willMessagePayloadBytes.byteLength +2;
}
if (this.userName != undefined)
remLength += UTF8Length(this.userName) + 2;
if (this.password != undefined)
remLength += UTF8Length(this.password) + 2;
break;
// Subscribe, Unsubscribe can both contain topic strings
case MESSAGE_TYPE.SUBSCRIBE:
first |= 0x02; // Qos = 1;
for ( var i = 0; i < this.topics.length; i++) {
topicStrLength[i] = UTF8Length(this.topics[i]);
remLength += topicStrLength[i] + 2;
}
remLength += this.requestedQos.length; // 1 byte for each topic's Qos
// QoS on Subscribe only
break;
case MESSAGE_TYPE.UNSUBSCRIBE:
first |= 0x02; // Qos = 1;
for ( var i = 0; i < this.topics.length; i++) {
topicStrLength[i] = UTF8Length(this.topics[i]);
remLength += topicStrLength[i] + 2;
}
break;
case MESSAGE_TYPE.PUBREL:
first |= 0x02; // Qos = 1;
break;
case MESSAGE_TYPE.PUBLISH:
if (this.payloadMessage.duplicate) first |= 0x08;
first = first |= (this.payloadMessage.qos << 1);
if (this.payloadMessage.retained) first |= 0x01;
destinationNameLength = UTF8Length(this.payloadMessage.destinationName);
remLength += destinationNameLength + 2;
var payloadBytes = this.payloadMessage.payloadBytes;
remLength += payloadBytes.byteLength;
if (payloadBytes instanceof ArrayBuffer)
payloadBytes = new Uint8Array(payloadBytes);
else if (!(payloadBytes instanceof Uint8Array))
payloadBytes = new Uint8Array(payloadBytes.buffer);
break;
case MESSAGE_TYPE.DISCONNECT:
break;
default:
;
}
// Now we can allocate a buffer for the message
var mbi = encodeMBI(remLength); // Convert the length to MQTT MBI format
var pos = mbi.length + 1; // Offset of start of variable header
var buffer = new ArrayBuffer(remLength + pos);
var byteStream = new Uint8Array(buffer); // view it as a sequence of bytes
//Write the fixed header into the buffer
byteStream[0] = first;
byteStream.set(mbi,1);
// If this is a PUBLISH then the variable header starts with a topic
if (this.type == MESSAGE_TYPE.PUBLISH)
pos = writeString(this.payloadMessage.destinationName, destinationNameLength, byteStream, pos);
// If this is a CONNECT then the variable header contains the protocol name/version, flags and keepalive time
else if (this.type == MESSAGE_TYPE.CONNECT) {
switch (this.mqttVersion) {
case 3:
byteStream.set(MqttProtoIdentifierv3, pos);
pos += MqttProtoIdentifierv3.length;
break;
case 4:
byteStream.set(MqttProtoIdentifierv4, pos);
pos += MqttProtoIdentifierv4.length;
break;
}
var connectFlags = 0;
if (this.cleanSession)
connectFlags = 0x02;
if (this.willMessage != undefined ) {
connectFlags |= 0x04;
connectFlags |= (this.willMessage.qos<<3);
if (this.willMessage.retained) {
connectFlags |= 0x20;
}
}
if (this.userName != undefined)
connectFlags |= 0x80;
if (this.password != undefined)
connectFlags |= 0x40;
byteStream[pos++] = connectFlags;
pos = writeUint16 (this.keepAliveInterval, byteStream, pos);
}
// Output the messageIdentifier - if there is one
if (this.messageIdentifier != undefined)
pos = writeUint16 (this.messageIdentifier, byteStream, pos);
switch(this.type) {
case MESSAGE_TYPE.CONNECT:
pos = writeString(this.clientId, UTF8Length(this.clientId), byteStream, pos);
if (this.willMessage != undefined) {
pos = writeString(this.willMessage.destinationName, UTF8Length(this.willMessage.destinationName), byteStream, pos);
pos = writeUint16(willMessagePayloadBytes.byteLength, byteStream, pos);
byteStream.set(willMessagePayloadBytes, pos);
pos += willMessagePayloadBytes.byteLength;
}
if (this.userName != undefined)
pos = writeString(this.userName, UTF8Length(this.userName), byteStream, pos);
if (this.password != undefined)
pos = writeString(this.password, UTF8Length(this.password), byteStream, pos);
break;
case MESSAGE_TYPE.PUBLISH:
// PUBLISH has a text or binary payload, if text do not add a 2 byte length field, just the UTF characters.
byteStream.set(payloadBytes, pos);
break;
// case MESSAGE_TYPE.PUBREC:
// case MESSAGE_TYPE.PUBREL:
// case MESSAGE_TYPE.PUBCOMP:
// break;
case MESSAGE_TYPE.SUBSCRIBE:
// SUBSCRIBE has a list of topic strings and request QoS
for (var i=0; i<this.topics.length; i++) {
pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos);
byteStream[pos++] = this.requestedQos[i];
}
break;
case MESSAGE_TYPE.UNSUBSCRIBE:
// UNSUBSCRIBE has a list of topic strings
for (var i=0; i<this.topics.length; i++)
pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos);
break;
default:
// Do nothing.
}
return buffer;
}
function decodeMessage(input,pos) {
var startingPos = pos;
var first = input[pos];
var type = first >> 4;
var messageInfo = first &= 0x0f;
pos += 1;
// Decode the remaining length (MBI format)
var digit;
var remLength = 0;
var multiplier = 1;
do {
if (pos == input.length) {
return [null,startingPos];
}
digit = input[pos++];
remLength += ((digit & 0x7F) * multiplier);
multiplier *= 128;
} while ((digit & 0x80) != 0);
var endPos = pos+remLength;
if (endPos > input.length) {
return [null,startingPos];
}
var wireMessage = new WireMessage(type);
switch(type) {
case MESSAGE_TYPE.CONNACK:
var connectAcknowledgeFlags = input[pos++];
if (connectAcknowledgeFlags & 0x01)
wireMessage.sessionPresent = true;
wireMessage.returnCode = input[pos++];
break;
case MESSAGE_TYPE.PUBLISH:
var qos = (messageInfo >> 1) & 0x03;
var len = readUint16(input, pos);
pos += 2;
var topicName = parseUTF8(input, pos, len);
pos += len;
// If QoS 1 or 2 there will be a messageIdentifier
if (qos > 0) {
wireMessage.messageIdentifier = readUint16(input, pos);
pos += 2;
}
var message = new Paho.MQTT.Message(input.subarray(pos, endPos));
if ((messageInfo & 0x01) == 0x01)
message.retained = true;
if ((messageInfo & 0x08) == 0x08)
message.duplicate = true;
message.qos = qos;
message.destinationName = topicName;
wireMessage.payloadMessage = message;
break;
case MESSAGE_TYPE.PUBACK:
case MESSAGE_TYPE.PUBREC:
case MESSAGE_TYPE.PUBREL:
case MESSAGE_TYPE.PUBCOMP:
case MESSAGE_TYPE.UNSUBACK:
wireMessage.messageIdentifier = readUint16(input, pos);
break;
case MESSAGE_TYPE.SUBACK:
wireMessage.messageIdentifier = readUint16(input, pos);
pos += 2;
wireMessage.returnCode = input.subarray(pos, endPos);
break;
default:
;
}
return [wireMessage,endPos];
}
function writeUint16(input, buffer, offset) {
buffer[offset++] = input >> 8; //MSB
buffer[offset++] = input % 256; //LSB
return offset;
}
function writeString(input, utf8Length, buffer, offset) {
offset = writeUint16(utf8Length, buffer, offset);
stringToUTF8(input, buffer, offset);
return offset + utf8Length;
}
function readUint16(buffer, offset) {
return 256*buffer[offset] + buffer[offset+1];
}
/**
* Encodes an MQTT Multi-Byte Integer
* @private
*/
function encodeMBI(number) {
var output = new Array(1);
var numBytes = 0;
do {
var digit = number % 128;
number = number >> 7;
if (number > 0) {
digit |= 0x80;
}
output[numBytes++] = digit;
} while ( (number > 0) && (numBytes<4) );
return output;
}
/**
* Takes a String and calculates its length in bytes when encoded in UTF8.
* @private
*/
function UTF8Length(input) {
var output = 0;
for (var i = 0; i<input.length; i++)
{
var charCode = input.charCodeAt(i);
if (charCode > 0x7FF)
{
// Surrogate pair means its a 4 byte character
if (0xD800 <= charCode && charCode <= 0xDBFF)
{
i++;
output++;
}
output +=3;
}
else if (charCode > 0x7F)
output +=2;
else
output++;
}
return output;
}
/**
* Takes a String and writes it into an array as UTF8 encoded bytes.
* @private
*/
function stringToUTF8(input, output, start) {
var pos = start;
for (var i = 0; i<input.length; i++) {
var charCode = input.charCodeAt(i);
// Check for a surrogate pair.
if (0xD800 <= charCode && charCode <= 0xDBFF) {
lowCharCode = input.charCodeAt(++i);
if (isNaN(lowCharCode)) {
throw new Error(format(ERROR.MALFORMED_UNICODE, [charCode, lowCharCode]));
}
charCode = ((charCode - 0xD800)<<10) + (lowCharCode - 0xDC00) + 0x10000;
}
if (charCode <= 0x7F) {
output[pos++] = charCode;
} else if (charCode <= 0x7FF) {
output[pos++] = charCode>>6 & 0x1F | 0xC0;
output[pos++] = charCode & 0x3F | 0x80;
} else if (charCode <= 0xFFFF) {
output[pos++] = charCode>>12 & 0x0F | 0xE0;
output[pos++] = charCode>>6 & 0x3F | 0x80;
output[pos++] = charCode & 0x3F | 0x80;
} else {
output[pos++] = charCode>>18 & 0x07 | 0xF0;
output[pos++] = charCode>>12 & 0x3F | 0x80;
output[pos++] = charCode>>6 & 0x3F | 0x80;
output[pos++] = charCode & 0x3F | 0x80;
};
}
return output;
}
function parseUTF8(input, offset, length) {
var output = "";
var utf16;
var pos = offset;
while (pos < offset+length)
{
var byte1 = input[pos++];
if (byte1 < 128)
utf16 = byte1;
else
{
var byte2 = input[pos++]-128;
if (byte2 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16),""]));
if (byte1 < 0xE0) // 2 byte character
utf16 = 64*(byte1-0xC0) + byte2;
else
{
var byte3 = input[pos++]-128;
if (byte3 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)]));
if (byte1 < 0xF0) // 3 byte character
utf16 = 4096*(byte1-0xE0) + 64*byte2 + byte3;
else
{
var byte4 = input[pos++]-128;
if (byte4 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
if (byte1 < 0xF8) // 4 byte character
utf16 = 262144*(byte1-0xF0) + 4096*byte2 + 64*byte3 + byte4;
else // longer encodings are not supported
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
}
}
}
if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair
{
utf16 -= 0x10000;
output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character
utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character
}
output += String.fromCharCode(utf16);
}
return output;
}
/**
* Repeat keepalive requests, monitor responses.
* @ignore
*/
var Pinger = function(client, window, keepAliveInterval) {
this._client = client;
this._window = window;
this._keepAliveInterval = keepAliveInterval*1000;
this.isReset = false;
var pingReq = new WireMessage(MESSAGE_TYPE.PINGREQ).encode();
var doTimeout = function (pinger) {
return function () {
return doPing.apply(pinger);
};
};
/** @ignore */
var doPing = function() {
if (!this.isReset) {
this._client._trace("Pinger.doPing", "Timed out");
this._client._disconnected( ERROR.PING_TIMEOUT.code , format(ERROR.PING_TIMEOUT));
} else {
this.isReset = false;
this._client._trace("Pinger.doPing", "send PINGREQ");
this._client.socket.send(pingReq);
this.timeout = this._window.setTimeout(doTimeout(this), this._keepAliveInterval);
}
}
this.reset = function() {
this.isReset = true;
this._window.clearTimeout(this.timeout);
if (this._keepAliveInterval > 0)
this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval);
}
this.cancel = function() {
this._window.clearTimeout(this.timeout);
}
};
/**
* Monitor request completion.
* @ignore
*/
var Timeout = function(client, window, timeoutSeconds, action, args) {
this._window = window;
if (!timeoutSeconds)
timeoutSeconds = 30;
var doTimeout = function (action, client, args) {
return function () {
return action.apply(client, args);
};
};
this.timeout = setTimeout(doTimeout(action, client, args), timeoutSeconds * 1000);
this.cancel = function() {
this._window.clearTimeout(this.timeout);
}
};
/*
* Internal implementation of the Websockets MQTT V3.1 client.
*
* @name Paho.MQTT.ClientImpl @constructor
* @param {String} host the DNS nameof the webSocket host.
* @param {Number} port the port number for that host.
* @param {String} clientId the MQ client identifier.
*/
var ClientImpl = function (uri, host, port, path, clientId) {
// Check dependencies are satisfied in this browser.
if (!("WebSocket" in global && global["WebSocket"] !== null)) {
throw new Error(format(ERROR.UNSUPPORTED, ["WebSocket"]));
}
if (!("localStorage" in global && global["localStorage"] !== null)) {
throw new Error(format(ERROR.UNSUPPORTED, ["localStorage"]));
}
if (!("ArrayBuffer" in global && global["ArrayBuffer"] !== null)) {
throw new Error(format(ERROR.UNSUPPORTED, ["ArrayBuffer"]));
}
this._trace("Paho.MQTT.Client", uri, host, port, path, clientId);
this.host = host;
this.port = port;
this.path = path;
this.uri = uri;
this.clientId = clientId;
// Local storagekeys are qualified with the following string.
// The conditional inclusion of path in the key is for backward
// compatibility to when the path was not configurable and assumed to
// be /mqtt
this._localKey=host+":"+port+(path!="/mqtt"?":"+path:"")+":"+clientId+":";
// Create private instance-only message queue
// Internal queue of messages to be sent, in sending order.
this._msg_queue = [];
// Messages we have sent and are expecting a response for, indexed by their respective message ids.
this._sentMessages = {};
// Messages we have received and acknowleged and are expecting a confirm message for
// indexed by their respective message ids.
this._receivedMessages = {};
// Internal list of callbacks to be executed when messages
// have been successfully sent over web socket, e.g. disconnect
// when it doesn't have to wait for ACK, just message is dispatched.
this._notify_msg_sent = {};
// Unique identifier for SEND messages, incrementing
// counter as messages are sent.
this._message_identifier = 1;
// Used to determine the transmission sequence of stored sent messages.
this._sequence = 0;
// Load the local state, if any, from the saved version, only restore state relevant to this client.
for (var key in localStorage)
if ( key.indexOf("Sent:"+this._localKey) == 0
|| key.indexOf("Received:"+this._localKey) == 0)
this.restore(key);
};
// Messaging Client public instance members.
ClientImpl.prototype.host;
ClientImpl.prototype.port;
ClientImpl.prototype.path;
ClientImpl.prototype.uri;
ClientImpl.prototype.clientId;
// Messaging Client private instance members.
ClientImpl.prototype.socket;
/* true once we have received an acknowledgement to a CONNECT packet. */
ClientImpl.prototype.connected = false;
/* The largest message identifier allowed, may not be larger than 2**16 but
* if set smaller reduces the maximum number of outbound messages allowed.
*/
ClientImpl.prototype.maxMessageIdentifier = 65536;
ClientImpl.prototype.connectOptions;
ClientImpl.prototype.hostIndex;
ClientImpl.prototype.onConnectionLost;
ClientImpl.prototype.onMessageDelivered;
ClientImpl.prototype.onMessageArrived;
ClientImpl.prototype._msg_queue = null;
ClientImpl.prototype._connectTimeout;
/* The sendPinger monitors how long we allow before we send data to prove to the server that we are alive. */
ClientImpl.prototype.sendPinger = null;
/* The receivePinger monitors how long we allow before we require evidence that the server is alive. */
ClientImpl.prototype.receivePinger = null;
ClientImpl.prototype.receiveBuffer = null;
ClientImpl.prototype._traceBuffer = null;
ClientImpl.prototype._MAX_TRACE_ENTRIES = 100;
ClientImpl.prototype.connect = function (connectOptions) {
var connectOptionsMasked = this._traceMask(connectOptions, "password");
this._trace("Client.connect", connectOptionsMasked, this.socket, this.connected);
if (this.connected)
throw new Error(format(ERROR.INVALID_STATE, ["already connected"]));
if (this.socket)
throw new Error(format(ERROR.INVALID_STATE, ["already connected"]));
this.connectOptions = connectOptions;
if (connectOptions.uris) {
this.hostIndex = 0;
this._doConnect(connectOptions.uris[0]);
} else {
this._doConnect(this.uri);
}
};
ClientImpl.prototype.subscribe = function (filter, subscribeOptions) {
this._trace("Client.subscribe", filter, subscribeOptions);
if (!this.connected)
throw new Error(format(ERROR.INVALID_STATE, ["not connected"]));
var wireMessage = new WireMessage(MESSAGE_TYPE.SUBSCRIBE);
wireMessage.topics=[filter];
if (subscribeOptions.qos != undefined)
wireMessage.requestedQos = [subscribeOptions.qos];
else
wireMessage.requestedQos = [0];
if (subscribeOptions.onSuccess) {
wireMessage.onSuccess = function(grantedQos) {subscribeOptions.onSuccess({invocationContext:subscribeOptions.invocationContext,grantedQos:grantedQos});};
}
if (subscribeOptions.onFailure) {
wireMessage.onFailure = function(errorCode) {subscribeOptions.onFailure({invocationContext:subscribeOptions.invocationContext,errorCode:errorCode});};
}
if (subscribeOptions.timeout) {
wireMessage.timeOut = new Timeout(this, window, subscribeOptions.timeout, subscribeOptions.onFailure
, [{invocationContext:subscribeOptions.invocationContext,
errorCode:ERROR.SUBSCRIBE_TIMEOUT.code,
errorMessage:format(ERROR.SUBSCRIBE_TIMEOUT)}]);
}
// All subscriptions return a SUBACK.
this._requires_ack(wireMessage);
this._schedule_message(wireMessage);
};
/** @ignore */
ClientImpl.prototype.unsubscribe = function(filter, unsubscribeOptions) {
this._trace("Client.unsubscribe", filter, unsubscribeOptions);
if (!this.connected)
throw new Error(format(ERROR.INVALID_STATE, ["not connected"]));
var wireMessage = new WireMessage(MESSAGE_TYPE.UNSUBSCRIBE);
wireMessage.topics = [filter];
if (unsubscribeOptions.onSuccess) {
wireMessage.callback = function() {unsubscribeOptions.onSuccess({invocationContext:unsubscribeOptions.invocationContext});};
}
if (unsubscribeOptions.timeout) {
wireMessage.timeOut = new Timeout(this, window, unsubscribeOptions.timeout, unsubscribeOptions.onFailure
, [{invocationContext:unsubscribeOptions.invocationContext,
errorCode:ERROR.UNSUBSCRIBE_TIMEOUT.code,
errorMessage:format(ERROR.UNSUBSCRIBE_TIMEOUT)}]);
}
// All unsubscribes return a SUBACK.
this._requires_ack(wireMessage);
this._schedule_message(wireMessage);
};
ClientImpl.prototype.send = function (message) {
this._trace("Client.send", message);
if (!this.connected)
throw new Error(format(ERROR.INVALID_STATE, ["not connected"]));
wireMessage = new WireMessage(MESSAGE_TYPE.PUBLISH);
wireMessage.payloadMessage = message;
if (message.qos > 0)
this._requires_ack(wireMessage);
else if (this.onMessageDelivered)
this._notify_msg_sent[wireMessage] = this.onMessageDelivered(wireMessage.payloadMessage);
this._schedule_message(wireMessage);
};
ClientImpl.prototype.disconnect = function () {
this._trace("Client.disconnect");
if (!this.socket)
throw new Error(format(ERROR.INVALID_STATE, ["not connecting or connected"]));
wireMessage = new WireMessage(MESSAGE_TYPE.DISCONNECT);
// Run the disconnected call back as soon as the message has been sent,
// in case of a failure later on in the disconnect processing.
// as a consequence, the _disconected call back may be run several times.
this._notify_msg_sent[wireMessage] = scope(this._disconnected, this);
this._schedule_message(wireMessage);
};
ClientImpl.prototype.getTraceLog = function () {
if ( this._traceBuffer !== null ) {
this._trace("Client.getTraceLog", new Date());
this._trace("Client.getTraceLog in flight messages", this._sentMessages.length);
for (var key in this._sentMessages)
this._trace("_sentMessages ",key, this._sentMessages[key]);
for (var key in this._receivedMessages)
this._trace("_receivedMessages ",key, this._receivedMessages[key]);
return this._traceBuffer;
}
};
ClientImpl.prototype.startTrace = function () {
if ( this._traceBuffer === null ) {
this._traceBuffer = [];
}
this._trace("Client.startTrace", new Date(), version);
};
ClientImpl.prototype.stopTrace = function () {
delete this._traceBuffer;
};
ClientImpl.prototype._doConnect = function (wsurl) {
// When the socket is open, this client will send the CONNECT WireMessage using the saved parameters.
if (this.connectOptions.useSSL) {
var uriParts = wsurl.split(":");
uriParts[0] = "wss";
wsurl = uriParts.join(":");
}
this.connected = false;
this.socket = new WebSocket(wsurl, ["mqtt","mqttv3.1"]);
this.socket.binaryType = 'arraybuffer';
this.socket.onopen = scope(this._on_socket_open, this);
this.socket.onmessage = scope(this._on_socket_message, this);
this.socket.onerror = scope(this._on_socket_error, this);
this.socket.onclose = scope(this._on_socket_close, this);
this.sendPinger = new Pinger(this, window, this.connectOptions.keepAliveInterval);
this.receivePinger = new Pinger(this, window, this.connectOptions.keepAliveInterval);
this._connectTimeout = new Timeout(this, window, this.connectOptions.timeout, this._disconnected, [ERROR.CONNECT_TIMEOUT.code, format(ERROR.CONNECT_TIMEOUT)]);
};
// Schedule a new message to be sent over the WebSockets
// connection. CONNECT messages cause WebSocket connection
// to be started. All other messages are queued internally
// until this has happened. When WS connection starts, process
// all outstanding messages.
ClientImpl.prototype._schedule_message = function (message) {
this._msg_queue.push(message);
// Process outstanding messages in the queue if we have an open socket, and have received CONNACK.
if (this.connected) {
this._process_queue();
}
};