1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import { RPCServer } from "../src/Backend";
- import { SubscriptionResponse, RPCInterface } from "../src/Types";
- import { RPCSocket } from "../src/Frontend";
- import { makeSubResponse } from "../src/Utils";
-
- type SubresExtension = {a:string}
-
- type MyInterface = {
- Group1: {
- triggerCallbacks: (...args:any[]) => Promise<void>,
- subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<SubresExtension>>,
- unsubscribe: (uuid:string) => Promise<void>
- },
- Group2: {
- echo: (x:string) => Promise<string>
- }
- }
-
- new RPCServer<SubresExtension, MyInterface>(20000,
- [{
- name: 'Group1',
- exportRPCs: () => [{
- name: 'triggerCallbacks',
- call: async () => { /*...*/ }
- },{
- name: 'subscribe',
- hook: async (param, callback) => { return makeSubResponse<SubresExtension>({a: "test"}) }
- },{
- name: 'unsubscribe',
- call: async(uuid) => { }
- }
- ]
- },{
- name: 'Group2',
- exportRPCs: () => [{
- name: 'echo',
- call: async (x) => "..."
- }]
- }]
- )
-
- 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")
- }))
|