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.

scratchpad.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { RPCSocket, RPCServer } from "./Index"
  2. // TL;DR
  3. const echo = (text: string) => {throw new Error("XD")}
  4. const add = (a: number, b: number) : number => a + b
  5. const getAsync = async () : Promise<{topic: string, message:string}>=> await new Promise((res, _) => {
  6. setTimeout(() => {
  7. res({
  8. topic: "Hey!!",
  9. message: "Hello World Async!"
  10. })
  11. }, 250)
  12. })
  13. const getCallback = (cbb: Function) : string => {
  14. setTimeout(() => {
  15. try{
  16. cbb({
  17. topic: "Hey!!",
  18. message: "Hello World Callback!"
  19. })
  20. }catch(e){
  21. console.log(String(e))
  22. }
  23. }, 250)
  24. return "Please wait for a callback :)"
  25. }
  26. new RPCServer(20000, [{
  27. name: 'MyRPCGroup1',
  28. exportRPCs: () => [
  29. echo,
  30. add,
  31. getAsync,
  32. {
  33. name: 'getCallback',
  34. hook: getCallback,
  35. onClose: (response, rpc) => { },
  36. onCallback: (...callbackArgs) => { }
  37. }
  38. ]
  39. }])
  40. new RPCSocket(20000, 'localhost').connect()
  41. .then(async sock => {
  42. try{
  43. const val = await sock.call('ABCD', 12345)
  44. console.log("VAL",val);
  45. }catch(e){
  46. console.log("RPC error "+e)
  47. }
  48. }).catch(e => console.log("connect err: "+e))