mirror of
https://github.com/Rolands-Laucis/Socio.git
synced 2026-05-15 14:15:57 -06:00
client instance url param now in opts dict
This commit is contained in:
parent
5d8bd544c3
commit
fb193a790f
3 changed files with 28 additions and 12 deletions
|
|
@ -291,10 +291,12 @@ When using SocioSecurity, but advised to always do this, the "socio" [JS Templat
|
||||||
import { SocioClient } from 'socio/dist/core-client.js';
|
import { SocioClient } from 'socio/dist/core-client.js';
|
||||||
import { socio } from 'socio/dist/utils';
|
import { socio } from 'socio/dist/utils';
|
||||||
|
|
||||||
//instantiate the Socio Client from lib on the expected websocket port and wait for it to connect
|
//instantiate the Socio Client from lib. The connection to the server is not established immediately, only if url is provided, allowing for more flexible setups.
|
||||||
//NB! use wss secure socket protocol (wss) and use the ./core/Secure class to encrypt these queries in PROD!
|
//NB! use wss secure socket protocol (wss) and use the ./core/Secure class to encrypt these queries in PROD!
|
||||||
const sc = new SocioClient(`ws://localhost:3000`, { logging: {verbose:true} }) ;//each instance is going to be its own "session" on the server, but you can spawn and destroy these where ever in your code
|
const sc = new SocioClient({ url: `ws://localhost:3000`, logging: {verbose:true} });
|
||||||
await sc.ready(); //wait until it has connected as confimed by the server
|
// otherwise, sc.Connect({url:'ws://localhost:3000'}) can be called any time later to start the connection with the socio server.
|
||||||
|
|
||||||
|
await sc.ready(); //wait until it has connected as confirmed by the server
|
||||||
|
|
||||||
sc.client_id; //can take a look at its ID, if that interests you idk
|
sc.client_id; //can take a look at its ID, if that interests you idk
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ export class AdminClient extends SocioClient {
|
||||||
//private:
|
//private:
|
||||||
#client_secret = '';
|
#client_secret = '';
|
||||||
|
|
||||||
constructor({ url='', client_secret = '', socio_client_opts }: AdminClientOptions){
|
constructor({ client_secret = '', socio_client_opts }: AdminClientOptions){
|
||||||
super(url, socio_client_opts);
|
super(socio_client_opts);
|
||||||
|
|
||||||
if (client_secret.length < 16)
|
if (client_secret.length < 16)
|
||||||
throw new E('client_secret length must be at least 16 char for safety. Got ', client_secret.length);
|
throw new E('client_secret length must be at least 16 char for safety. Got ', client_secret.length);
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,8 @@ export type ProgressOnUpdate = (percentage: number) => void;
|
||||||
|
|
||||||
type PropUpdateCallback = ((new_val: PropValue, diff?: diff_lib.rdiffResult[]) => void) | null;
|
type PropUpdateCallback = ((new_val: PropValue, diff?: diff_lib.rdiffResult[]) => void) | null;
|
||||||
export type ClientProp = { val: PropValue | undefined, subs: { [id: id]: PropUpdateCallback } };
|
export type ClientProp = { val: PropValue | undefined, subs: { [id: id]: PropUpdateCallback } };
|
||||||
export type SocioClientOptions = {
|
export type SocioClientOptions = {
|
||||||
|
url?: string,
|
||||||
name?: string,
|
name?: string,
|
||||||
keep_alive?: boolean,
|
keep_alive?: boolean,
|
||||||
reconnect_tries?: number,
|
reconnect_tries?: number,
|
||||||
|
|
@ -35,6 +36,7 @@ export type SocioClientOptions = {
|
||||||
hooks?: Partial<ClientLifecycleHooks>,
|
hooks?: Partial<ClientLifecycleHooks>,
|
||||||
allow_rpc?:boolean,
|
allow_rpc?:boolean,
|
||||||
} & LoggingOpts;
|
} & LoggingOpts;
|
||||||
|
type ConnectOptions = { url?: string, keep_alive?: boolean, reconnect_tries?: number };
|
||||||
|
|
||||||
type DiscoveryBy = 'ID' | 'NAME' | 'AS_ARRAY';
|
type DiscoveryBy = 'ID' | 'NAME' | 'AS_ARRAY';
|
||||||
type DiscoveryReturn = {
|
type DiscoveryReturn = {
|
||||||
|
|
@ -49,7 +51,7 @@ export class SocioClient extends LogHandler {
|
||||||
// private:
|
// private:
|
||||||
#ws: WebSocket | null = null;
|
#ws: WebSocket | null = null;
|
||||||
#client_id:ClientID = '';
|
#client_id:ClientID = '';
|
||||||
#latency:number;
|
#latency:number = 0;
|
||||||
#is_ready: Function | boolean = false;
|
#is_ready: Function | boolean = false;
|
||||||
#authenticated=false;
|
#authenticated=false;
|
||||||
|
|
||||||
|
|
@ -67,7 +69,8 @@ export class SocioClient extends LogHandler {
|
||||||
//discon has to be an async function, such that you may await the new ready(), but socio wont wait for it to finish.
|
//discon has to be an async function, such that you may await the new ready(), but socio wont wait for it to finish.
|
||||||
// progs: Map<Promise<any>, number> = new Map(); //the promise is that of a socio generic data going out from client async. Number is WS send buffer payload size at the time of query
|
// progs: Map<Promise<any>, number> = new Map(); //the promise is that of a socio generic data going out from client async. Number is WS send buffer payload size at the time of query
|
||||||
|
|
||||||
constructor(url: string, {
|
constructor({
|
||||||
|
url,
|
||||||
name,
|
name,
|
||||||
logging = { verbose: false, hard_crash: false },
|
logging = { verbose: false, hard_crash: false },
|
||||||
keep_alive = true,
|
keep_alive = true,
|
||||||
|
|
@ -79,20 +82,31 @@ export class SocioClient extends LogHandler {
|
||||||
super({ ...logging, prefix: name ? `SocioClient:${name}` : 'SocioClient' });
|
super({ ...logging, prefix: name ? `SocioClient:${name}` : 'SocioClient' });
|
||||||
|
|
||||||
// public:
|
// public:
|
||||||
this.config = { name, logging, keep_alive, reconnect_tries, persistent, allow_rpc };
|
this.config = { url, name, logging, keep_alive, reconnect_tries, persistent, allow_rpc };
|
||||||
this.lifecycle_hooks = { ...initLifecycleHooks<ClientLifecycleHooks>(), ...hooks };
|
this.lifecycle_hooks = { ...initLifecycleHooks<ClientLifecycleHooks>(), ...hooks };
|
||||||
|
|
||||||
// private:
|
// connect right away if url provided, or the user can call Connect later manually. This is useful for creating the class variable when the page is not loaded yet
|
||||||
|
if(url){
|
||||||
|
this.Connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Connect({ url = this.config?.url, keep_alive = this.config?.keep_alive || false, reconnect_tries = this.config?.reconnect_tries || 0 }: ConnectOptions = {}) {
|
||||||
|
// checks
|
||||||
|
if(!url) throw new E('Must provide a WebSocket URL to connect to! [#no-url]');
|
||||||
|
if(this.#ws && this.#ws.readyState === WebSocket.OPEN) throw new E('Socio WebSocket is already connected! Please disconnect first before connecting again or create a new instance. [#already-connected]');
|
||||||
|
|
||||||
this.#latency = (new Date()).getTime();
|
this.#latency = (new Date()).getTime();
|
||||||
this.#connect(url, keep_alive, this.verbose || false, reconnect_tries);
|
this.#connect(url, keep_alive, this.verbose || false, reconnect_tries);
|
||||||
|
|
||||||
// log info for the dev
|
// log info for the dev
|
||||||
if(this.verbose){
|
if (this.verbose) {
|
||||||
if (window && url.startsWith('ws://'))
|
if (window && url.startsWith('ws://'))
|
||||||
this.HandleInfo('WARNING, UNSECURE WEBSOCKET URL CONNECTION! Please use wss:// and https:// protocols in production to protect against man-in-the-middle attacks. You need to host an https server with bought SCTs - Signed Certificate Timestamps (keys) - from an authority.');
|
this.HandleInfo('WARNING, UNSECURE WEBSOCKET URL CONNECTION! Please use wss:// and https:// protocols in production to protect against man-in-the-middle attacks. You need to host an https server with bought SCTs - Signed Certificate Timestamps (keys) - from an authority.');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
return this.ready();
|
||||||
|
}
|
||||||
|
|
||||||
async #connect(url: string, keep_alive: boolean, verbose: boolean, reconnect_tries:number){
|
async #connect(url: string, keep_alive: boolean, verbose: boolean, reconnect_tries:number){
|
||||||
this.#ws = new WebSocket(url);
|
this.#ws = new WebSocket(url);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue