BookUpdateCheckerController.js 3.4 KB

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