mirror of
https://github.com/Rolands-Laucis/Socio.git
synced 2026-05-15 14:15:57 -06:00
new send data notation structure
This commit is contained in:
parent
8409d70748
commit
1cb4d48131
2 changed files with 36 additions and 39 deletions
|
|
@ -49,11 +49,11 @@ export class SocioClient {
|
|||
|
||||
switch(kind){
|
||||
case 'CON':
|
||||
this.#ses_id = data;
|
||||
this.#is_ready();
|
||||
this.#ses_id = data;
|
||||
this.#is_ready(true); //resolve promise to true
|
||||
if (this.verbose) done(`WebSocket connected.`, this.name);
|
||||
// delete this.#is_ready; //clear memory. Cannot delete private properties
|
||||
this.#is_ready = null;
|
||||
|
||||
this.#is_ready = undefined; //clear memory. Cannot delete private properties
|
||||
break;
|
||||
case 'UPD':
|
||||
if (this.#FindID(kind, data?.id))
|
||||
|
|
@ -89,9 +89,10 @@ export class SocioClient {
|
|||
ready(){return new Promise(res => this.#is_ready = res)}
|
||||
|
||||
//private method
|
||||
#send(data={}){
|
||||
this.#ws.send(JSON.stringify({ client_id: this.#ses_id, ...data }))
|
||||
if (this.verbose) info('sent:', data)
|
||||
#send(kind='', ...data){ //data is an array of parameters to this func, where every element (after first) is an object. First param can also not be an object in some cases
|
||||
if(data.length < 1) soft_error('Not enough arguments to send data! kind;data:', kind, ...data) //the first argument must always be the data to send. Other params may be objects with aditional keys to be added in the future
|
||||
this.#ws.send(JSON.stringify(Object.assign({}, { client_id: this.#ses_id, kind: kind, data:data[0] }, ...data.slice(1))))
|
||||
if (this.verbose) info('sent:', kind, data)
|
||||
}
|
||||
|
||||
//subscribe to an sql query. Can add multiple callbacks where ever in your code, if their sql queries are identical
|
||||
|
|
@ -103,7 +104,7 @@ export class SocioClient {
|
|||
else{
|
||||
const id = this.#gen_key
|
||||
this.#queries[id] = { sql: sql, f: [t ? callback.bind(t) : callback] }
|
||||
this.#send({ kind: 'REG', data:{ id: id, sql: sql, params: params }})
|
||||
this.#send('REG', { id: id, sql: sql, params: params })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -114,13 +115,13 @@ export class SocioClient {
|
|||
this.#queries[id] = res
|
||||
})
|
||||
//send off the request, which will be resolved in the message handler
|
||||
this.#send({kind:'SQL', data:{ id:id, sql: sql, params: params }})
|
||||
this.#send('SQL', { id: id, sql: sql, params: params })
|
||||
return await prom
|
||||
}
|
||||
|
||||
//sends a ping with either the user provided number or an auto generated number, for keeping track of packets and debugging
|
||||
ping(num=0){
|
||||
this.#send({kind:'PING', data:{ id: num || this.#gen_key }})
|
||||
this.#send('PING', { id: num || this.#gen_key })
|
||||
}
|
||||
|
||||
async authenticate(params={}){ //params here can be anything, like username and password stuff etc. The backend server auth function callback will receive this entire object
|
||||
|
|
@ -129,7 +130,7 @@ export class SocioClient {
|
|||
const prom = new Promise((res) => {
|
||||
this.#queries[id] = res
|
||||
})
|
||||
this.#send({kind:'AUTH', data:{ id:id, params: params}})
|
||||
this.#send('AUTH', { id: id, params: params })
|
||||
return await prom
|
||||
}
|
||||
|
||||
|
|
|
|||
52
core/core.js
52
core/core.js
|
|
@ -58,7 +58,7 @@ export class SessionManager{
|
|||
this.#lifecycle_hooks.con(this.#sessions[client_id], req)
|
||||
|
||||
//notify the client of their ID
|
||||
this.#sessions[client_id].Send('CON', { data:client_id });
|
||||
this.#sessions[client_id].Send('CON', client_id);
|
||||
this.#HandleInfo('CON', client_id)
|
||||
|
||||
//set this client websockets event handlers
|
||||
|
|
@ -100,15 +100,13 @@ export class SessionManager{
|
|||
switch (kind) {
|
||||
case 'REG':
|
||||
if (client_id in this.#sessions)
|
||||
this.#sessions[client_id].Send('UPD',{
|
||||
data:{
|
||||
id: data.id,
|
||||
result: await this.Query({
|
||||
...data,
|
||||
ses_id: this.#sessions[client_id].ses_id
|
||||
})
|
||||
}
|
||||
this.#sessions[client_id].Send('UPD', {
|
||||
id: data.id,
|
||||
result: await this.Query({
|
||||
...data,
|
||||
ses_id: this.#sessions[client_id].ses_id
|
||||
})
|
||||
})
|
||||
|
||||
//set up hook
|
||||
if (QueryIsSelect(data.sql))
|
||||
|
|
@ -121,7 +119,7 @@ export class SessionManager{
|
|||
//have to do the query in every case
|
||||
const res = this.Query({ ...data, ses_id: this.#sessions[client_id].ses_id })
|
||||
if (is_select) //wait for result, if a result is expected, and send it back
|
||||
this.#sessions[client_id].Send('SQL',{data:{ id: data.id, result: await res } })
|
||||
this.#sessions[client_id].Send('SQL', { id: data.id, result: await res })
|
||||
}
|
||||
|
||||
//if the sql wasnt a SELECT, but altered some resource, then need to propogate that to other connection hooks
|
||||
|
|
@ -129,12 +127,12 @@ export class SessionManager{
|
|||
this.Update(ParseSQLForTables(data.sql))
|
||||
|
||||
break;
|
||||
case 'PING': this.#sessions[client_id].Send('PONG',{data:{ id: data?.id }}); break;
|
||||
case 'PING': this.#sessions[client_id].Send('PONG', { id: data?.id }); break;
|
||||
case 'AUTH':
|
||||
if (this.#lifecycle_hooks.auth)
|
||||
this.#sessions[client_id].Send('AUTH',{data:{ id: data.id, result: await this.#lifecycle_hooks.auth(client_id, data.params) } })
|
||||
this.#sessions[client_id].Send('AUTH', { id: data.id, result: await this.#lifecycle_hooks.auth(client_id, data.params) })
|
||||
else
|
||||
this.#sessions[client_id].Send('AUTH', {data:{ id: data.id, result: false } })
|
||||
this.#sessions[client_id].Send('AUTH', { id: data.id, result: false })
|
||||
break;
|
||||
// case '': break;
|
||||
default: throw (`Unrecognized message kind! [${kind}] with data:`, data);
|
||||
|
|
@ -154,18 +152,15 @@ export class SessionManager{
|
|||
tables.forEach(async (t) => {
|
||||
if (s.hook_tables.includes(t)) {
|
||||
for await (const hook of s.GetHookObjs(t)) {
|
||||
s.Send('UPD', {
|
||||
data:
|
||||
{
|
||||
id: hook.id,
|
||||
result: (await this.Query(
|
||||
{
|
||||
ses_id: s.ses_id,
|
||||
...hook
|
||||
}))
|
||||
}
|
||||
}
|
||||
)
|
||||
s.Send('UPD', {
|
||||
id: hook.id,
|
||||
result: (await this.Query(
|
||||
{
|
||||
ses_id: s.ses_id,
|
||||
...hook
|
||||
}))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
@ -177,7 +172,7 @@ export class SessionManager{
|
|||
SendTo(client_id='', data={}){
|
||||
try{
|
||||
if (client_id in this.#sessions)
|
||||
this.#sessions[client_id].Send('PUSH', {data:data })
|
||||
this.#sessions[client_id].Send('PUSH', data)
|
||||
else throw `The provided session ID [${client_id}] was not found in the tracked web socket connections!`
|
||||
} catch (e) { this.#HandleError(e) }
|
||||
}
|
||||
|
|
@ -244,8 +239,9 @@ class Session{
|
|||
// log('reg hook', table, this.#hooks[table])
|
||||
}
|
||||
|
||||
Send(kind='',data={}){
|
||||
this.#ws.send(JSON.stringify({ kind: kind, ...data }))
|
||||
Send(kind = '', ...data) {//data is an array of parameters to this func, where every element (after first) is an object. First param can also not be an object in some cases
|
||||
if (data.length < 1) soft_error('Not enough arguments to send data! kind;data:', kind, ...data) //the first argument must always be the data to send. Other params may be objects with aditional keys to be added in the future
|
||||
this.#ws.send(JSON.stringify(Object.assign({}, { kind: kind, data: data[0] }, ...data.slice(1))))
|
||||
if (this.verbose) info('sent:',kind, data)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue