123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import { RPCServer } from '../src/Backend'
- import { SubscriptionResponse, ErrorResponse, SuccessResponse } from '../src/Types'
- import { HookRPC } from '../src/Types'
- import * as uuidv4 from "uuid/v4"
- import { makeSubResponse } from '../src/Utils'
-
- let subcallback
-
- new RPCServer(20000, [{
- name: "HelloWorldRPCGroup",
- exportRPCs: () => [
- {
- name: 'echo',
- call: async (s:string) => s,
- },{
- name: 'simpleSubscribe',
- hook: async(callback) => {
- subcallback = callback
- return makeSubResponse()
- }
- },{
- name: 'subscribe',
- hook: async (callback):Promise<any> => {
- subcallback = callback
- return makeSubResponse()
- },
- onClose: (res, rpc) => {
- console.log("Specific close handler for", rpc.name, res)
- subcallback = null
- },
- onCallback: (...args) => { console.log.apply(console, args) }
- },
- function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
- function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
- ]
- }])
-
- new RPCServer<{ topic: string }>(20001, [{
- name: "HelloWorldRPCGroup",
- exportRPCs: () => [
- {
- name: 'echo',
- call: async (s:string) => s,
- },{
- name: 'simpleSubscribe',
- hook: async(callback) => {
- subcallback = callback
- return {
- result: "Success",
- uuid: uuidv4(),
- topic: ""
- }
- }
- },{
- name: 'subscribe',
- hook: async (callback) => {
- subcallback = callback
- return {
- result: "Success",
- uuid: uuidv4(),
- topic: ""
- }
- },
- onClose: (res, rpc) => {
- console.log("Specific close handler for", rpc.name, res)
- subcallback = null
- },
- onCallback: (...args:any) => { console.log.apply(console, args) }
- },
- function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
- function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
- ]
- }])
-
- try{
- new RPCServer(20001, [{
- name: "bad",
- exportRPCs: () => [
- (aaa,bbb,ccc) => { return aaa+bbb+ccc }
- ]
- }])
- }catch(badRPCError){
- console.log("expected bad-RPC error happened: "+ !!badRPCError, String(badRPCError))
- }
|