Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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