diff --git a/Documentation.md b/Documentation.md index 7c675f6..f745d96 100644 --- a/Documentation.md +++ b/Documentation.md @@ -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: diff --git a/core/core-server.ts b/core/core-server.ts index c8db222..c56ed96 100644 --- a/core/core-server.ts +++ b/core/core-server.ts @@ -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 } }); } diff --git a/core/package-lock.json b/core/package-lock.json index 2ae0656..16fcaa3 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -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", diff --git a/core/package.json b/core/package.json index d9e5d20..33196d7 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/core/types.d.ts b/core/types.d.ts index 575a27d..6c8dbd7 100644 --- a/core/types.d.ts +++ b/core/types.d.ts @@ -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; export type Con_Hook = (caller_client: SocioSession, request: IncomingMessage) => void | Promise; export type Discon_Hook = (caller_client: SocioSession) => void | Promise; @@ -45,6 +64,7 @@ export type FileDownload_Hook = (caller_client: SocioSession, data: any) => FS_U export type Upd_Hook = (sessions: Map, initiator: SocioSession, sql: string, params:object|null) => boolean | Promise; export type Endpoint_Hook = (caller_client: SocioSession, endpoint: string) => string | Promise; export type Gen_Prop_Name_Hook = () => string | Promise; +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;