12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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")
- }))
-
- const srv = new RPCServer(30000, [{
- name: 'Group2',
- exportRPCs: () => [{
- name: 'echo',
- call: async (x) => x
- }]
- }], {
- sesame: 'open'
- })
-
- const s = new RPCSocket(30000, 'localhost')
- s.connect("open").then(async() => {
- s['Group2']['echo']('open', 'dfgfg').then(console.log)
- s['Group2']['echo']('dfgfg').then(console.log)
-
- s['Group2']['echo']('dfgfg').then(console.log)
-
- })
|