BookUpdateCheckerController.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.accessToken = config.accessToken;
  13. this.bucServer = new BUCServer(config);
  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. if (req.accessToken !== this.accessToken)
  48. throw new Error('Access denied');
  49. switch (req.action) {
  50. case 'test':
  51. await this.test(req, ws); break;
  52. case 'get-buc':
  53. await this.getBuc(req, ws); break;
  54. case 'update-buc':
  55. await this.updateBuc(req, ws); break;
  56. default:
  57. throw new Error(`Action not found: ${req.action}`);
  58. }
  59. } catch (e) {
  60. this.send({error: e.message}, req, ws);
  61. }
  62. }
  63. send(res, req, ws) {
  64. if (ws.readyState == WebSocket.OPEN) {
  65. ws.lastActivity = Date.now();
  66. let r = res;
  67. if (req.requestId)
  68. r = Object.assign({requestId: req.requestId}, r);
  69. const message = JSON.stringify(r);
  70. ws.send(message);
  71. if (this.isDevelopment) {
  72. log(`WebSocket-OUT: ${message.substr(0, 4000)}`);
  73. }
  74. }
  75. }
  76. //Actions ------------------------------------------------------------------
  77. async test(req, ws) {
  78. this.send({message: 'Liberama project is awesome'}, req, ws);
  79. }
  80. async getBuc(req, ws) {
  81. if (!req.fromCheckTime)
  82. throw new Error(`key 'fromCheckTime' is empty`);
  83. await this.bucServer.getBuc(req.fromCheckTime, (rows) => {
  84. this.send({state: 'get', rows}, req, ws);
  85. });
  86. this.send({state: 'finish'}, req, ws);
  87. }
  88. async updateBuc(req, ws) {
  89. if (!req.bookUrls)
  90. throw new Error(`key 'bookUrls' is empty`);
  91. if (!Array.isArray(req.bookUrls))
  92. throw new Error(`key 'bookUrls' must be array`);
  93. await this.bucServer.updateBuc(req.bookUrls);
  94. this.send({state: 'success'}, req, ws);
  95. }
  96. }
  97. module.exports = BookUpdateCheckerController;