1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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<void>,
- subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<{a: string}>>,
- unsubscribe: (uuid:string) => Promise<void>
- },
- Group2: {
- echo: (x:string) => Promise<string>
- }
- }>
-
- const callbacks:Map<string, Function> = new Map()
-
- new RPCServer<{a:string}, MyInterface>(20000,
- [{
- name: "Group1",
- exportRPCs: () => [
- <HookRPC<MyInterface['Group1'], 'subscribe', {a:string}>>{
- name: 'subscribe',
- hook: async (param, callback) => { const _uuid = ""+Math.random(); console.log(param); callbacks.set(_uuid, callback); return { result: 'Success', a: '3', uuid: _uuid} }
- },
- <CallRPC<MyInterface['Group1'], 'unsubscribe'>>{
- name: 'unsubscribe',
- call: async (uuid) => { callbacks.delete(uuid) }
- },
- <CallRPC<MyInterface['Group1'], 'triggerCallbacks'>>{
- name: 'triggerCallbacks',
- call: async (...args) => { callbacks.forEach(cb => cb.apply({}, args)) }
- }]
- },{
- name: 'Group2',
- exportRPCs: () => [
- <CallRPC<MyInterface['Group2'], 'echo'>>{
- 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<MyInterface>(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<MyInterface>(20001, 'localhost').then((async (client) => {
- console.log(client)
- const r = await client.Group2.echo("hee")
- console.log(r)
- }))
|