DiscoverSessions by format

This commit is contained in:
Rolands 2024-12-06 19:25:08 +01:00
parent 1239763f5a
commit 55f5c7b8e3
5 changed files with 23 additions and 6 deletions

View file

@ -599,7 +599,7 @@ const sc = new SocioClient(`ws://${location.hostname}:3000`, { logging: {verbose
await sc.ready(); await sc.ready();
// this gives {client_id:{name, ip}} at the time of writing this, but can be any data // 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: There is also a server hook for customizing the returned discovery info:

View file

@ -424,10 +424,21 @@ export class SocioClient extends LogHandler {
Ping(id_num = undefined) { Ping(id_num = undefined) {
this.Send(ServerMessageKind.PING, { id: typeof id_num === 'number' ? id_num : this.GenKey }); 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(); const { id, prom } = this.CreateQueryPromise();
this.Send(ServerMessageKind.DISCOVERY, { id }); this.Send(ServerMessageKind.DISCOVERY, { id });
return prom as Promise<discovery_resp_obj>; let clients = await (prom as Promise<discovery_resp_obj>);
// 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 } = {}) { UnsubscribeAll({ props = true, queries = true, force = false } = {}) {
if (props) if (props)

View file

@ -1,12 +1,12 @@
{ {
"name": "socio", "name": "socio",
"version": "1.13.0", "version": "1.13.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "socio", "name": "socio",
"version": "1.13.0", "version": "1.13.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",

View file

@ -1,6 +1,6 @@
{ {
"name": "socio", "name": "socio",
"version": "1.13.0", "version": "1.13.1",
"description": "A WebSocket Real-Time Communication (RTC) API framework.", "description": "A WebSocket Real-Time Communication (RTC) API framework.",
"main": "./dist/core.js", "main": "./dist/core.js",
"type": "module", "type": "module",

6
package-lock.json generated Normal file
View file

@ -0,0 +1,6 @@
{
"name": "Socio",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}