From 55f5c7b8e31b9cadf4ad3dff5ce5bbb98e9c0dcd Mon Sep 17 00:00:00 2001 From: Rolands Date: Fri, 6 Dec 2024 19:25:08 +0100 Subject: [PATCH] DiscoverSessions by format --- Documentation.md | 2 +- core/core-client.ts | 15 +++++++++++++-- core/package-lock.json | 4 ++-- core/package.json | 2 +- package-lock.json | 6 ++++++ 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 package-lock.json diff --git a/Documentation.md b/Documentation.md index 1012a62..116f8ae 100644 --- a/Documentation.md +++ b/Documentation.md @@ -599,7 +599,7 @@ const sc = new SocioClient(`ws://${location.hostname}:3000`, { logging: {verbose await sc.ready(); // this gives {client_id:{name, ip}} at the time of writing this, but can be any data -await sc.DiscoverSessions(); +await sc.DiscoverSessions('ID' | 'NAME' | 'AS_ARRAY'); //for convenience, u can retrieve this data in either of 3 built in formats. Default formatted object by ID ``` There is also a server hook for customizing the returned discovery info: diff --git a/core/core-client.ts b/core/core-client.ts index 7a8b82d..a2810e9 100644 --- a/core/core-client.ts +++ b/core/core-client.ts @@ -424,10 +424,21 @@ export class SocioClient extends LogHandler { Ping(id_num = undefined) { this.Send(ServerMessageKind.PING, { id: typeof id_num === 'number' ? id_num : this.GenKey }); } - DiscoverSessions(){ + async DiscoverSessions(by: 'ID' | 'NAME' | 'AS_ARRAY' = 'ID'){ const { id, prom } = this.CreateQueryPromise(); this.Send(ServerMessageKind.DISCOVERY, { id }); - return prom as Promise; + let clients = await (prom as Promise); + + // format it for convenience + switch(by){ + case 'NAME': { + return Object.fromEntries(Object.entries(clients).map(([id, meta]) => [meta?.name ? meta.name : id, { ...meta, id }])); + } + case 'AS_ARRAY': { + return Object.entries(clients).map(([id, meta]) => { return { ...meta, id }}); + } + default: return clients; + } } UnsubscribeAll({ props = true, queries = true, force = false } = {}) { if (props) diff --git a/core/package-lock.json b/core/package-lock.json index 3cdd43f..6f86171 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -1,12 +1,12 @@ { "name": "socio", - "version": "1.13.0", + "version": "1.13.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "socio", - "version": "1.13.0", + "version": "1.13.1", "license": "MIT", "dependencies": { "js-yaml": "^4.1.0", diff --git a/core/package.json b/core/package.json index 2261d2d..d5883cd 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "socio", - "version": "1.13.0", + "version": "1.13.1", "description": "A WebSocket Real-Time Communication (RTC) API framework.", "main": "./dist/core.js", "type": "module", diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1d9cc3c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "Socio", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}