identify server hook

This commit is contained in:
Rolands 2024-12-04 21:09:10 +01:00
parent 97ef3fc6b1
commit 070ba005c8
5 changed files with 38 additions and 4 deletions

View file

@ -597,6 +597,16 @@ await sc.ready();
//name it anything, but it must be a unique name on the server currently, otherwise you get an error msg
await sc.IdentifySelf(`Main ${new Date().toISOString()}`);
```
There is also a server hook for being notified of a client identifying itself, bcs this turns out to be very convenient in code:
```ts
//server code
socserv.RegisterLifecycleHookHandler('identify', (caller_client: SocioSession, name:string) => {
// name is the new name this caller_client has been assigned.
// this only gets called if the identification was successful.
// the client session name property has already been set
// return void
})
```
##### Network Discovery
Enable it on the server:

View file

@ -566,6 +566,10 @@ export class SocioServer extends LogHandler {
client.Send(ClientMessageKind.RES, { id: data.id, result: { success: 0, error: 'A session already has this name!' } });
}else{
client.name = name;
if (this.#lifecycle_hooks?.identify)
this.#lifecycle_hooks.identify(client, name);
client.Send(ClientMessageKind.RES, { id: data.id, result: { success: 1 } });
}

View file

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

View file

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

22
core/types.d.ts vendored
View file

@ -28,7 +28,26 @@ export type SessionOpts = { session_timeout_ttl_ms: number, max_payload_size?: n
export type ClientSubscribeOpts = { sql?: string, endpoint?: string, params?: object | null };
//server hook functions
export type ServerLifecycleHooks = { con?: Con_Hook, discon?: Discon_Hook, msg?: Msg_Hook, sub?: Sub_Hook, unsub?: Unsub_Hook, upd?: Upd_Hook, auth?: Auth_Hook, gen_client_id?: GenCLientID_Hook, grant_perm?: GrantPerm_Hook, serv?: Serv_Hook, admin?: Admin_Hook, blob?: Blob_Hook, file_upload?: FileUpload_Hook, file_download?: FileDownload_Hook, endpoint?: Endpoint_Hook, gen_prop_name?: Gen_Prop_Name_Hook, discovery?: Discovery_Hook };
export type ServerLifecycleHooks = {
con?: Con_Hook,
discon?: Discon_Hook,
msg?: Msg_Hook,
sub?: Sub_Hook,
unsub?: Unsub_Hook,
upd?: Upd_Hook,
auth?: Auth_Hook,
gen_client_id?: GenCLientID_Hook,
grant_perm?: GrantPerm_Hook,
serv?: Serv_Hook,
admin?: Admin_Hook,
blob?: Blob_Hook,
file_upload?: FileUpload_Hook,
file_download?: FileDownload_Hook,
endpoint?: Endpoint_Hook,
gen_prop_name?: Gen_Prop_Name_Hook,
identify?: Identify_Hook,
discovery?: Discovery_Hook
};
export type GenCLientID_Hook = () => ClientID | Promise<ClientID>;
export type Con_Hook = (caller_client: SocioSession, request: IncomingMessage) => void | Promise<void>;
export type Discon_Hook = (caller_client: SocioSession) => void | Promise<void>;
@ -45,6 +64,7 @@ export type FileDownload_Hook = (caller_client: SocioSession, data: any) => FS_U
export type Upd_Hook = (sessions: Map<ClientID, SocioSession>, initiator: SocioSession, sql: string, params:object|null) => boolean | Promise<boolean>;
export type Endpoint_Hook = (caller_client: SocioSession, endpoint: string) => string | Promise<string>;
export type Gen_Prop_Name_Hook = () => string | Promise<string>;
export type Identify_Hook = (caller_client: SocioSession, name:string) => void;
type discovery_resp_obj = { [client_id: string]: { name?: string, ip: string } };
export type Discovery_Hook = (caller_client: SocioSession) => discovery_resp_obj | any;
// export type _Hook = (client: SocioSession) => boolean;