You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { RPCServer } from '../src/Backend'
  2. import { SubscriptionResponse, ErrorResponse, SuccessResponse } from '../src/Types'
  3. import { HookRPC } from '../src/Types'
  4. import * as uuidv4 from "uuid/v4"
  5. import { makeSubResponse } from '../src/Utils'
  6. let subcallback
  7. new RPCServer(20000, [{
  8. name: "HelloWorldRPCGroup",
  9. exportRPCs: () => [
  10. {
  11. name: 'echo',
  12. call: async (s:string) => s,
  13. },{
  14. name: 'simpleSubscribe',
  15. hook: async(callback) => {
  16. subcallback = callback
  17. return makeSubResponse()
  18. }
  19. },{
  20. name: 'subscribe',
  21. hook: async (callback):Promise<any> => {
  22. subcallback = callback
  23. return makeSubResponse()
  24. },
  25. onClose: (res, rpc) => {
  26. console.log("Specific close handler for", rpc.name, res)
  27. subcallback = null
  28. },
  29. onCallback: (...args) => { console.log.apply(console, args) }
  30. },
  31. function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
  32. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  33. ]
  34. }])
  35. new RPCServer<{ topic: string }>(20001, [{
  36. name: "HelloWorldRPCGroup",
  37. exportRPCs: () => [
  38. {
  39. name: 'echo',
  40. call: async (s:string) => s,
  41. },{
  42. name: 'simpleSubscribe',
  43. hook: async(callback) => {
  44. subcallback = callback
  45. return {
  46. result: "Success",
  47. uuid: uuidv4(),
  48. topic: ""
  49. }
  50. }
  51. },{
  52. name: 'subscribe',
  53. hook: async (callback) => {
  54. subcallback = callback
  55. return {
  56. result: "Success",
  57. uuid: uuidv4(),
  58. topic: ""
  59. }
  60. },
  61. onClose: (res, rpc) => {
  62. console.log("Specific close handler for", rpc.name, res)
  63. subcallback = null
  64. },
  65. onCallback: (...args:any) => { console.log.apply(console, args) }
  66. },
  67. function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
  68. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  69. ]
  70. }])
  71. try{
  72. new RPCServer(20001, [{
  73. name: "bad",
  74. exportRPCs: () => [
  75. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  76. ]
  77. }])
  78. }catch(badRPCError){
  79. console.log("expected bad-RPC error happened: "+ !!badRPCError, String(badRPCError))
  80. }