@@ -49,6 +49,7 @@ import CBOR from 'cbor-js';
49
49
import ArduinoCloudError from './ArduinoCloudError' ;
50
50
51
51
const connections = { } ;
52
+ const subscribedTopics = { } ;
52
53
const propertyCallback = { } ;
53
54
const arduinoCloudPort = 8443 ;
54
55
const arduinoCloudHost = 'wss.iot.arduino.cc' ;
@@ -78,6 +79,7 @@ const connect = options => new Promise((resolve, reject) => {
78
79
token : options . token ,
79
80
onDisconnect : options . onDisconnect ,
80
81
onTrace : options . onTrace ,
82
+ onConnected : options . onConnected ,
81
83
} ;
82
84
83
85
if ( ! opts . host ) {
@@ -134,6 +136,22 @@ const connect = options => new Promise((resolve, reject) => {
134
136
}
135
137
} ;
136
138
139
+ client . onConnected = ( reconnect ) => {
140
+ if ( reconnect === true ) {
141
+ // This is a re-connection: re-subscribe to all topics subscribed before the
142
+ // connection loss
143
+ Object . getOwnPropertySymbols ( subscribedTopics ) . forEach ( ( connectionId ) => {
144
+ Object . values ( subscribedTopics [ connectionId ] ) . forEach ( ( subscribeParams ) => {
145
+ subscribe ( connectionId , subscribeParams . topic , subscribeParams . cb )
146
+ } ) ;
147
+ } ) ;
148
+ }
149
+
150
+ if ( typeof opts . onConnected === 'function' ) {
151
+ opts . onConnected ( reconnect )
152
+ }
153
+ } ;
154
+
137
155
if ( typeof onDisconnect === 'function' ) {
138
156
client . onConnectionLost = opts . onDisconnect ;
139
157
}
@@ -346,6 +364,15 @@ const onPropertyValue = (connectionId, thingId, name, cb) => {
346
364
}
347
365
const propOutputTopic = `/a/t/${ thingId } /e/o` ;
348
366
367
+ if ( ! subscribedTopics [ connectionId ] ) {
368
+ subscribedTopics [ connectionId ] = { } ;
369
+ }
370
+
371
+ subscribedTopics [ connectionId ] [ thingId ] = {
372
+ topic : propOutputTopic ,
373
+ cb : cb ,
374
+ } ;
375
+
349
376
if ( ! propertyCallback [ propOutputTopic ] ) {
350
377
propertyCallback [ propOutputTopic ] = { } ;
351
378
propertyCallback [ propOutputTopic ] [ name ] = cb ;
0 commit comments