Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

devtest.ts 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { RPCServer } from "../src/Backend";
  2. import { SubscriptionResponse, CallRPC, HookRPC, RPCInterface } from "../src/Types";
  3. import { RPCSocket } from "../src/Frontend";
  4. type MyInterface = RPCInterface<{
  5. Group1: {
  6. triggerCallbacks: (...args:any[]) => Promise<void>,
  7. subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<{a: string}>>,
  8. unsubscribe: (uuid:string) => Promise<void>
  9. },
  10. Group2: {
  11. echo: (x:string) => Promise<string>
  12. }
  13. }>
  14. const callbacks:Map<string, Function> = new Map()
  15. new RPCServer<{a:string}, MyInterface>(20000,
  16. [{
  17. name: "Group1",
  18. exportRPCs: () => [
  19. <HookRPC<MyInterface['Group1'], 'subscribe', {a:string}>>{
  20. name: 'subscribe',
  21. hook: async (param, callback) => { const _uuid = ""+Math.random(); console.log(param); callbacks.set(_uuid, callback); return { result: 'Success', a: '3', uuid: _uuid} }
  22. },
  23. <CallRPC<MyInterface['Group1'], 'unsubscribe'>>{
  24. name: 'unsubscribe',
  25. call: async (uuid) => { callbacks.delete(uuid) }
  26. },
  27. <CallRPC<MyInterface['Group1'], 'triggerCallbacks'>>{
  28. name: 'triggerCallbacks',
  29. call: async (...args) => { callbacks.forEach(cb => cb.apply({}, args)) }
  30. }]
  31. },{
  32. name: 'Group2',
  33. exportRPCs: () => [
  34. <CallRPC<MyInterface['Group2'], 'echo'>>{
  35. name: 'echo',
  36. call: async (x) => x
  37. }]
  38. }]
  39. )
  40. new RPCServer<{a:string}, MyInterface>(20001,
  41. [{
  42. name: "Group1",
  43. exportRPCs: () => []
  44. },{
  45. name: 'Group2',
  46. exportRPCs: () => [{
  47. name: 'echo',
  48. call: async (x) => x+" lol"
  49. }]
  50. }]
  51. )
  52. RPCSocket.makeSocket<MyInterface>(20000, 'localhost').then((async (client) => {
  53. console.log(client)
  54. const res = await client.Group1.subscribe('test', async (...args:any) => {
  55. console.log.apply(console, args)
  56. /* close the callbacks once you're done */
  57. await client.Group1.unsubscribe(res.uuid)
  58. client.unhook(res.uuid)
  59. })
  60. await client.Group1.triggerCallbacks("Hello", "World", "Callbacks")
  61. }))
  62. RPCSocket.makeSocket<MyInterface>(20001, 'localhost').then((async (client) => {
  63. console.log(client)
  64. const r = await client.Group2.echo("hee")
  65. console.log(r)
  66. }))