選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestBackend.ts 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { RPCServer } from '../src/Backend'
  2. import { SubscriptionResponse, ErrorResponse, SuccessResponse } from '../src/Responses'
  3. import { HookRPC } from '../src/Types'
  4. let subcallback
  5. new RPCServer(20000, [{
  6. name: "HelloWorldRPCGroup",
  7. exportRPCs: () => [
  8. {
  9. name: 'echo',
  10. call: async (s:string) => s,
  11. },{
  12. name: 'simpleSubscribe',
  13. hook: async(callback) => {
  14. subcallback = callback
  15. return new SubscriptionResponse(""+Math.random())
  16. }
  17. },{
  18. name: 'subscribe',
  19. hook: async (callback):Promise<any> => {
  20. subcallback = callback
  21. return new SubscriptionResponse(""+Math.random())
  22. },
  23. onClose: (res:SubscriptionResponse, rpc:HookRPC) => {
  24. console.log("Specific close handler for", rpc.name, res)
  25. subcallback = null
  26. },
  27. onCallback: (...args) => { console.log.apply(console, args) }
  28. },
  29. function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
  30. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  31. ]
  32. }])
  33. try{
  34. new RPCServer(20001, [{
  35. name: "bad",
  36. exportRPCs: () => [
  37. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  38. ]
  39. }])
  40. }catch(badRPCError){
  41. console.log("expected bad-RPC error happened: "+ !!badRPCError, String(badRPCError))
  42. }