WebSocketController.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const WebSocket = require ('ws');
  2. const _ = require('lodash');
  3. const WorkerState = require('../core/WorkerState');//singleton
  4. const WebWorker = require('../core/WebWorker');//singleton
  5. const log = new (require('../core/AppLogger'))().log;//singleton
  6. //const utils = require('../core/utils');
  7. const cleanPeriod = 1*60*1000;//1 минута
  8. const closeSocketOnIdle = 5*60*1000;//5 минут
  9. class WebSocketController {
  10. constructor(wss, config) {
  11. this.config = config;
  12. this.isDevelopment = (config.branch == 'development');
  13. this.workerState = new WorkerState();
  14. this.webWorker = new WebWorker(config);
  15. this.wss = wss;
  16. wss.on('connection', (ws) => {
  17. ws.on('message', (message) => {
  18. this.onMessage(ws, message.toString());
  19. });
  20. ws.on('error', (err) => {
  21. log(LM_ERR, err);
  22. });
  23. });
  24. setTimeout(() => { this.periodicClean(); }, cleanPeriod);
  25. }
  26. periodicClean() {
  27. try {
  28. const now = Date.now();
  29. this.wss.clients.forEach((ws) => {
  30. if (!ws.lastActivity || now - ws.lastActivity > closeSocketOnIdle - 50) {
  31. ws.terminate();
  32. }
  33. });
  34. } finally {
  35. setTimeout(() => { this.periodicClean(); }, cleanPeriod);
  36. }
  37. }
  38. async onMessage(ws, message) {
  39. let req = {};
  40. try {
  41. if (this.isDevelopment) {
  42. log(`WebSocket-IN: ${message.substr(0, 4000)}`);
  43. }
  44. req = JSON.parse(message);
  45. ws.lastActivity = Date.now();
  46. //pong for WebSocketConnection
  47. this.send({_rok: 1}, req, ws);
  48. switch (req.action) {
  49. case 'test':
  50. await this.test(req, ws); break;
  51. case 'get-config':
  52. await this.getConfig(req, ws); break;
  53. case 'get-worker-state':
  54. await this.getWorkerState(req, ws); break;
  55. default:
  56. throw new Error(`Action not found: ${req.action}`);
  57. }
  58. } catch (e) {
  59. this.send({error: e.message}, req, ws);
  60. }
  61. }
  62. send(res, req, ws) {
  63. if (ws.readyState == WebSocket.OPEN) {
  64. ws.lastActivity = Date.now();
  65. let r = res;
  66. if (req.requestId)
  67. r = Object.assign({requestId: req.requestId}, r);
  68. const message = JSON.stringify(r);
  69. ws.send(message);
  70. if (this.isDevelopment) {
  71. log(`WebSocket-OUT: ${message.substr(0, 4000)}`);
  72. }
  73. }
  74. }
  75. //Actions ------------------------------------------------------------------
  76. async test(req, ws) {
  77. this.send({message: `${this.config.name} project is awesome`}, req, ws);
  78. }
  79. async getConfig(req, ws) {
  80. if (Array.isArray(req.params)) {
  81. const paramsSet = new Set(req.params);
  82. this.send(_.pick(this.config, this.config.webConfigParams.filter(x => paramsSet.has(x))), req, ws);
  83. } else {
  84. throw new Error('params is not an array');
  85. }
  86. }
  87. async getWorkerState(req, ws) {
  88. if (!req.workerId)
  89. throw new Error(`key 'workerId' is empty`);
  90. const state = this.workerState.getState(req.workerId);
  91. this.send((state ? state : {}), req, ws);
  92. }
  93. }
  94. module.exports = WebSocketController;