Add Session.genRequestNumDiscriminator()

This commit is contained in:
2022-08-29 16:12:31 +02:00
parent 494e297224
commit 7a0a2dc3e5

View File

@ -21,7 +21,7 @@ interface DomainServerJson {
interface OutputEnvelopeJson { interface OutputEnvelopeJson {
status: string, status: string,
requestNr: number, request_nr: number,
data: any, data: any,
action: string | null | undefined, action: string | null | undefined,
error: { error: {
@ -156,6 +156,7 @@ export class Session {
httpBaseUrl: string | null; httpBaseUrl: string | null;
websocket: WebSocket | null; websocket: WebSocket | null;
numRequests: number; numRequests: number;
requestNumDiscriminator: number;
subscriptions: { subscriptions: {
callback: (a: EventJson) => void, callback: (a: EventJson) => void,
requestNr: number | undefined, requestNr: number | undefined,
@ -168,6 +169,7 @@ export class Session {
constructor(domain: Domain) { constructor(domain: Domain) {
this.domain = domain; this.domain = domain;
this.numRequests = 0; this.numRequests = 0;
this.requestNumDiscriminator = Session.genRequestNumDiscriminator();
this.server = null; this.server = null;
this.token = null; this.token = null;
this.httpBaseUrl = null; this.httpBaseUrl = null;
@ -176,6 +178,14 @@ export class Session {
this.subscriptionEndpoints = []; 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) { async close(keepEndpoints: boolean = false) {
await this.unsubscribeAll(keepEndpoints); await this.unsubscribeAll(keepEndpoints);
if (this.websocket && (this.websocket.readyState !== WebSocket.CLOSING && this.websocket.readyState !== WebSocket.CLOSED)) { if (this.websocket && (this.websocket.readyState !== WebSocket.CLOSING && this.websocket.readyState !== WebSocket.CLOSED)) {
@ -303,7 +313,7 @@ export class Session {
while (true) { while (true) {
try { try {
if (!forceHttp && this.websocket !== null) { if (!forceHttp && this.websocket !== null) {
const req_nr = this.numRequests; const req_nr = this.numRequests + this.requestNumDiscriminator;
const startTime = performance.now(); const startTime = performance.now();
await this.waitForWebSocket(timeout); await this.waitForWebSocket(timeout);
@ -421,7 +431,6 @@ export class Session {
} }
private async _subscribe(cb: (a: EventJson) => void) { private async _subscribe(cb: (a: EventJson) => void) {
this.numRequests++;
if (this.websocket !== null) { if (this.websocket !== null) {
const subscription = await this.send('subscribe', {}, 60_000, false, undefined, (res) => { const subscription = await this.send('subscribe', {}, 60_000, false, undefined, (res) => {
if (res.action === 'push') { if (res.action === 'push') {