BookUpdateCheckerController.js 3.4 KB

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