diff --git a/package-lock.json b/package-lock.json index ba68a40..7c1c5aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -538,9 +538,9 @@ "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==" }, "@types/node": { - "version": "12.12.6", - "resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-12.12.6.tgz", - "integrity": "sha512-FjsYUPzEJdGXjwKqSpE0/9QEh6kzhTAeObA54rn6j3rR4C/mzpI9L0KNfoeASSPMMdxIsoJuCLDWcM/rVjIsSA==" + "version": "12.12.7", + "resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-12.12.7.tgz", + "integrity": "sha512-E6Zn0rffhgd130zbCbAr/JdXfXkoOUFAKNs/rF8qnafSJ8KYaA/j3oz7dcwal+lYjLA7xvdd5J4wdYpCTlP8+w==" }, "@types/range-parser": { "version": "1.2.3", diff --git a/package.json b/package.json index 851ca50..ea7e925 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@gitzone/tsbuild": "^2.1.17", "@gitzone/tstest": "^1.0.28", "@pushrocks/tapbundle": "^3.0.13", - "@types/node": "^12.12.6", + "@types/node": "^12.12.7", "tslint": "^5.20.1", "tslint-config-prettier": "^1.18.0" }, diff --git a/test/test.ts b/test/test.ts index 8c781e9..ad04ea1 100644 --- a/test/test.ts +++ b/test/test.ts @@ -31,7 +31,8 @@ tap.test('add a message to the SmartUniverse', async () => { // testing message handling tap.test('create smartuniverse client', async () => { testClientUniverse = new smartuniverse.ClientUniverse({ - serverAddress: testServerData.serverAddress + serverAddress: testServerData.serverAddress, + autoReconnect: true }); expect(testClientUniverse).to.be.instanceof(smartuniverse.ClientUniverse); }); @@ -65,7 +66,8 @@ tap.test('universe should contain the sent message', async () => { tap.test('a second client should be able to subscibe', async () => { testClientUniverse2 = new smartuniverse.ClientUniverse({ - serverAddress: testServerData.serverAddress + serverAddress: testServerData.serverAddress, + autoReconnect: true }); testClientUniverse2.addChannel(testChannelData.channelName, testChannelData.channelPass); diff --git a/ts/smartuniverse.classes.clientuniverse.ts b/ts/smartuniverse.classes.clientuniverse.ts index 54bde21..d3da4f1 100644 --- a/ts/smartuniverse.classes.clientuniverse.ts +++ b/ts/smartuniverse.classes.clientuniverse.ts @@ -12,6 +12,7 @@ import { ClientUniverseCache } from './smartuniverse.classes.clientuniversecache export interface IClientOptions { serverAddress: string; + autoReconnect: boolean; } /** @@ -165,12 +166,14 @@ export class ClientUniverse { } public async disconnect(reason: 'upstreamEvent' | 'triggered' = 'triggered', tryReconnect = false) { - if ('triggered') { - this.smartsocketClient.disconnect(); + if (reason === 'triggered') { + const smartsocketToDisconnect = this.smartsocketClient; + this.smartsocketClient = null; // making sure the upstreamEvent does not interfere + await smartsocketToDisconnect.disconnect(); } - this.smartsocketClient = null; - if (tryReconnect) { + if (this.options.autoReconnect && reason === 'upstreamEvent' && this.smartsocketClient) { await plugins.smartdelay.delayForRandom(5000, 20000); + this.smartsocketClient = null; this.checkConnection(); } } diff --git a/ts/smartuniverse.classes.universeconnection.ts b/ts/smartuniverse.classes.universeconnection.ts index f4c6784..83775a3 100644 --- a/ts/smartuniverse.classes.universeconnection.ts +++ b/ts/smartuniverse.classes.universeconnection.ts @@ -121,6 +121,7 @@ export class UniverseConnection { socketConnection: plugins.smartsocket.SocketConnection; authenticationRequests: Array; }) { + this.universeRef = optionsArg.universe; this.authenticationRequests = optionsArg.authenticationRequests; this.socketConnection = optionsArg.socketConnection; this.socketConnection.eventSubject.subscribe(async eventArg => {