import { RPCServer } from "../src/Backend"; import { SubscriptionResponse, CallRPC, HookRPC, RPCInterface } from "../src/Types"; import { RPCSocket } from "../src/Frontend"; type MyInterface = RPCInterface<{ Group1: { triggerCallbacks: (...args:any[]) => Promise, subscribe: (param:string, callback:Function) => Promise>, unsubscribe: (uuid:string) => Promise }, Group2: { echo: (x:string) => Promise } }> const callbacks:Map = new Map() new RPCServer<{a:string}, MyInterface>(20000, [{ name: "Group1", exportRPCs: () => [ >{ name: 'subscribe', hook: async (param, callback) => { const _uuid = ""+Math.random(); console.log(param); callbacks.set(_uuid, callback); return { result: 'Success', a: '3', uuid: _uuid} } }, >{ name: 'unsubscribe', call: async (uuid) => { callbacks.delete(uuid) } }, >{ name: 'triggerCallbacks', call: async (...args) => { callbacks.forEach(cb => cb.apply({}, args)) } }] },{ name: 'Group2', exportRPCs: () => [ >{ name: 'echo', call: async (x) => x }] }] ) new RPCServer<{a:string}, MyInterface>(20001, [{ name: "Group1", exportRPCs: () => [] },{ name: 'Group2', exportRPCs: () => [{ name: 'echo', call: async (x) => x+" lol" }] }] ) RPCSocket.makeSocket(20000, 'localhost').then((async (client) => { console.log(client) const res = await client.Group1.subscribe('test', async (...args:any) => { console.log.apply(console, args) /* close the callbacks once you're done */ await client.Group1.unsubscribe(res.uuid) client.unhook(res.uuid) }) await client.Group1.triggerCallbacks("Hello", "World", "Callbacks") })) RPCSocket.makeSocket(20001, 'localhost').then((async (client) => { console.log(client) const r = await client.Group2.echo("hee") console.log(r) }))