test123
This commit is contained in:
+3
-3
@@ -54,7 +54,7 @@ function makeServer(){
|
||||
|
||||
|
||||
describe('RPCServer', () => {
|
||||
let server: RPCServer<{ topic: string}>
|
||||
let server: RPCServer<{ topic: string }, any>
|
||||
|
||||
before((done) => {
|
||||
server = makeServer()
|
||||
@@ -160,8 +160,8 @@ describe('It should do unhook', () => {
|
||||
name: "test",
|
||||
exportRPCs: () => [{
|
||||
name: 'subscribe',
|
||||
hook: async(callback) => {
|
||||
cb = callback
|
||||
hook: async(callback):Promise<SubscriptionResponse<{topic:string}>> => {
|
||||
cb = <Function> callback
|
||||
return {
|
||||
result: "Success",
|
||||
uuid: uuidv4(),
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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)
|
||||
}))
|
||||
Reference in New Issue
Block a user