Skip to content

Commit 1fd085c

Browse files
committed
Add possibility to turn auto reconnect off
1 parent 2d38255 commit 1fd085c

3 files changed

Lines changed: 27 additions & 23 deletions

File tree

README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ result.additionalCredentials()
157157
Parameter parameter = [];
158158
}
159159
160-
studio.api.Client(uri, notificationListener)
161-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160+
studio.api.Client(uri, notificationListener, autoConnect)
161+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162162

163163
- Arguments
164164

@@ -167,6 +167,8 @@ studio.api.Client(uri, notificationListener)
167167
notificationListener - Object returning two functions: applicationAcceptanceRequested(studio.api.Request) and credentialsRequested(studio.api.Request).
168168
Function applicationAcceptanceRequested must return a Promise of void. Can be used to popup system use notification message and ask for confirmation to continue.
169169
Function credentialsRequested must return a Promise of dictionary containing 'Username' and 'Password' as keys for authentication.
170+
171+
autoConnect - Tries to reconnect once disconnected. By default is enabled.
170172

171173
- Returns
172174

index.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ studio.protocol = (function(ProtoBuf) {
230230

231231
this.handle = function(message){
232232
return new Promise(function(resolve, reject) {
233-
234233
try {
235234
var hello = obj.Hello.decode(message);
236235
} catch (err) {
@@ -555,16 +554,16 @@ studio.internal = (function(proto) {
555554
var connecting = false;
556555
var this_ = this;
557556

558-
this.onAppConnect = function(url, notificationListener) {
557+
this.onAppConnect = function(url, notificationListener, autoConnect) {
559558
return new Promise(function (resolve, reject) {
560-
var appConnection = new obj.AppConnection(url, notificationListener);
559+
var appConnection = new obj.AppConnection(url, notificationListener, autoConnect);
561560
appConnections.push(appConnection);
562561
var sys = appConnection.root();
563562
sys.async.onDone(resolve, reject, sys);
564563
});
565564
};
566565

567-
this.onConnect = function(resolve, reject) {
566+
this.onConnect = function(resolve, reject, autoConnect) {
568567
if (connected) {
569568
resolve(this_);
570569
return;
@@ -578,13 +577,13 @@ studio.internal = (function(proto) {
578577
connecting = true;
579578
pendingConnects.push({resolve: resolve, reject: reject});
580579

581-
this.onAppConnect(studioURL, notificationListener).then(function(system){
580+
this.onAppConnect(studioURL, notificationListener, autoConnect).then(function(system){
582581
var promises = [];
583582
system.forEachChild(function (app) {
584583
if (!app.info().is_local)
585584
{
586585
var appUrl = app.info().server_addr + ":" + app.info().server_port;
587-
promises.push(this_.onAppConnect(appUrl, notificationListener));
586+
promises.push(this_.onAppConnect(appUrl, notificationListener, autoConnect));
588587
}
589588
});
590589
Promise.all(promises).then(function() {
@@ -711,7 +710,7 @@ studio.internal = (function(proto) {
711710
};
712711
};
713712

714-
obj.AppConnection = function(url, notificationListener) {
713+
obj.AppConnection = function(url, notificationListener, autoConnect) {
715714
var appConnection = this;
716715
var appName = "";
717716
var appId = undefined;
@@ -783,17 +782,20 @@ studio.internal = (function(proto) {
783782

784783
console.log("Socket close: " + reason);
785784

786-
setTimeout(function () {
787-
console.log("Trying to reconnect", appUrl);
788-
socket = new WebSocket(appUrl);
789-
handler = new proto.Handler(socket, notificationListener);
790-
handler.onContainer = handleIncomingContainer;
791-
socket.binaryType = proto.BINARY_TYPE;
792-
socket.onopen = onOpen;
793-
socket.onclose = onClosed;
794-
socket.onmessage = onMessage;
795-
socket.onerror = onError;
796-
}, 3000);
785+
if (autoConnect)
786+
{
787+
setTimeout(function () {
788+
console.log("Trying to reconnect", appUrl);
789+
socket = new WebSocket(appUrl);
790+
handler = new proto.Handler(socket, notificationListener);
791+
handler.onContainer = handleIncomingContainer;
792+
socket.binaryType = proto.BINARY_TYPE;
793+
socket.onopen = onOpen;
794+
socket.onclose = onClosed;
795+
socket.onmessage = onMessage;
796+
socket.onerror = onError;
797+
}, 3000);
798+
}
797799
};
798800

799801
socket.onopen = onOpen;
@@ -1364,7 +1366,7 @@ studio.api = (function(internal) {
13641366
* @this Client
13651367
* @constructor
13661368
*/
1367-
obj.Client = function(studioURL, notificationListener) {
1369+
obj.Client = function(studioURL, notificationListener, autoConnect = true) {
13681370
var system = new internal.SystemNode(studioURL, notificationListener);
13691371

13701372
/**
@@ -1376,7 +1378,7 @@ studio.api = (function(internal) {
13761378
return new Promise(function(resolve, reject) {
13771379
system.onConnect(function(system){
13781380
resolve(new INode(system));
1379-
}, reject);
1381+
}, reject, autoConnect);
13801382
});
13811383
};
13821384

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cdp-client",
3-
"version": "2.1.1",
3+
"version": "2.2.2",
44
"description": "A simple Javascript interface for the CDP Studio development platform that allows Javascript applications to interact with",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)