Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Backend.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict'
  2. import http = require('http');
  3. import bsock = require('bsock');
  4. import * as T from './Types';
  5. import * as U from './Utils';
  6. import * as I from './Interfaces';
  7. /**
  8. * A Websocket-server-on-steroids with built-in RPC capabilities
  9. */
  10. export class RPCServer<
  11. SubResType = {}
  12. > implements I.Destroyable{
  13. private ws = http.createServer()
  14. private io = bsock.createServer()
  15. private visibility:T.Visibility
  16. private closeHandler:T.CloseHandler
  17. private errorHandler: T.ErrorHandler
  18. private connectionHandler: T.ConnectionHandler
  19. /**
  20. * @throws On RPC with no name
  21. * @param port The port to listen on
  22. * @param exporters A list of {@link RPCExporter} to publish
  23. * @param conf A {@link SocketConf} object with optional settings
  24. */
  25. constructor(
  26. private port:number,
  27. private exporters: I.RPCExporter<SubResType>[] = [],
  28. conf: T.SocketConf = {}
  29. ){
  30. if(!conf.visibility) this.visibility = "127.0.0.1"
  31. this.errorHandler = (socket:I.Socket) => (error:any) => {
  32. if(conf.errorHandler) conf.errorHandler(socket, error)
  33. socket.destroy();
  34. console.error("Caught websocket error", String(error))
  35. }
  36. this.closeHandler = (socket:I.Socket) => {
  37. if(!conf.closeHandler) console.log("Socket on port "+socket.port+" closing")
  38. else conf.closeHandler(socket)
  39. }
  40. this.connectionHandler = (socket:I.Socket) => {
  41. if(!conf.connectionHandler) console.log("New websocket connection on port "+socket.port)
  42. else conf.connectionHandler(socket)
  43. }
  44. let badRPC
  45. if(badRPC = exporters.flatMap(ex => ex.exportRPCs()).find(rpc => !rpc.name))
  46. throw new Error(`
  47. RPC did not provide a name.
  48. \nUse 'funtion name(..){ .. }' syntax instead.
  49. \n
  50. \n<------------OFFENDING RPC:
  51. \n`+badRPC.toString()+`
  52. \n>------------OFFENDING RPC`)
  53. this.startWebsocket()
  54. }
  55. private startWebsocket(){
  56. try{
  57. this.io.attach(this.ws)
  58. this.io.on('socket', (socket:I.Socket) => {
  59. socket.on('error', (err) => this.errorHandler(socket, err))
  60. socket.on('close', () => this.closeHandler(socket))
  61. this.connectionHandler(socket)
  62. this.initRPCs(socket)
  63. })
  64. this.ws.listen(this.port, this.visibility)
  65. }catch(e){
  66. //@ts-ignore
  67. this.errorHandler(undefined)("Unable to connect to socket")
  68. }
  69. }
  70. protected initRPCs(socket:I.Socket){
  71. socket.hook('info', () => rpcInfos)
  72. const rpcInfos:T.ExtendedRpcInfo[] = [
  73. ...this.exporters.flatMap(exporter => U.rpcHooker(socket, exporter))
  74. ]
  75. }
  76. async destroy(): Promise<void> {
  77. this.io.close()
  78. this.ws.close()
  79. }
  80. }