From 7a0a2dc3e5e2e028918db487ce87308a16199b7c Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 29 Aug 2022 16:12:31 +0200 Subject: [PATCH] Add Session.genRequestNumDiscriminator() --- src/usimp.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/usimp.ts b/src/usimp.ts index b893887..6c3eaec 100644 --- a/src/usimp.ts +++ b/src/usimp.ts @@ -21,7 +21,7 @@ interface DomainServerJson { interface OutputEnvelopeJson { status: string, - requestNr: number, + request_nr: number, data: any, action: string | null | undefined, error: { @@ -156,6 +156,7 @@ export class Session { httpBaseUrl: string | null; websocket: WebSocket | null; numRequests: number; + requestNumDiscriminator: number; subscriptions: { callback: (a: EventJson) => void, requestNr: number | undefined, @@ -168,6 +169,7 @@ export class Session { constructor(domain: Domain) { this.domain = domain; this.numRequests = 0; + this.requestNumDiscriminator = Session.genRequestNumDiscriminator(); this.server = null; this.token = null; this.httpBaseUrl = null; @@ -176,6 +178,14 @@ export class Session { this.subscriptionEndpoints = []; } + private static genRequestNumDiscriminator(): number { + // JS uses 64-bit double precision floating point numbers. + // These can accurately represent integers up to 53 bit. + // In JS bitwise operators only operate on 32-bit integers. + // Here a 48-bit integer number is generated. + return Math.floor(Math.random() * 0x1000000) * 0x1000000; + } + async close(keepEndpoints: boolean = false) { await this.unsubscribeAll(keepEndpoints); if (this.websocket && (this.websocket.readyState !== WebSocket.CLOSING && this.websocket.readyState !== WebSocket.CLOSED)) { @@ -303,7 +313,7 @@ export class Session { while (true) { try { if (!forceHttp && this.websocket !== null) { - const req_nr = this.numRequests; + const req_nr = this.numRequests + this.requestNumDiscriminator; const startTime = performance.now(); await this.waitForWebSocket(timeout); @@ -421,7 +431,6 @@ export class Session { } private async _subscribe(cb: (a: EventJson) => void) { - this.numRequests++; if (this.websocket !== null) { const subscription = await this.send('subscribe', {}, 60_000, false, undefined, (res) => { if (res.action === 'push') {