ParseWebSocketServer.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ParseWebSocketServer = exports.ParseWebSocket = void 0;
  6. var _AdapterLoader = require("../Adapters/AdapterLoader");
  7. var _WSAdapter = require("../Adapters/WebSocketServer/WSAdapter");
  8. var _logger = _interopRequireDefault(require("../logger"));
  9. var _events = _interopRequireDefault(require("events"));
  10. var _util = require("util");
  11. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  12. class ParseWebSocketServer {
  13. constructor(server, onConnect, config) {
  14. config.server = server;
  15. const wss = (0, _AdapterLoader.loadAdapter)(config.wssAdapter, _WSAdapter.WSAdapter, config);
  16. wss.onListen = () => {
  17. _logger.default.info('Parse LiveQuery Server started running');
  18. };
  19. wss.onConnection = ws => {
  20. ws.waitingForPong = false;
  21. ws.on('pong', () => {
  22. ws.waitingForPong = false;
  23. });
  24. ws.on('error', error => {
  25. _logger.default.error(error.message);
  26. _logger.default.error((0, _util.inspect)(ws, false));
  27. });
  28. onConnect(new ParseWebSocket(ws));
  29. // Send ping to client periodically
  30. const pingIntervalId = setInterval(() => {
  31. if (!ws.waitingForPong) {
  32. ws.ping();
  33. ws.waitingForPong = true;
  34. } else {
  35. clearInterval(pingIntervalId);
  36. ws.terminate();
  37. }
  38. }, config.websocketTimeout || 10 * 1000);
  39. };
  40. wss.onError = error => {
  41. _logger.default.error(error);
  42. };
  43. wss.start();
  44. this.server = wss;
  45. }
  46. close() {
  47. if (this.server && this.server.close) {
  48. this.server.close();
  49. }
  50. }
  51. }
  52. exports.ParseWebSocketServer = ParseWebSocketServer;
  53. class ParseWebSocket extends _events.default.EventEmitter {
  54. constructor(ws) {
  55. super();
  56. ws.onmessage = request => this.emit('message', request && request.data ? request.data : request);
  57. ws.onclose = () => this.emit('disconnect');
  58. this.ws = ws;
  59. }
  60. send(message) {
  61. this.ws.send(message);
  62. }
  63. }
  64. exports.ParseWebSocket = ParseWebSocket;
  65. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfQWRhcHRlckxvYWRlciIsInJlcXVpcmUiLCJfV1NBZGFwdGVyIiwiX2xvZ2dlciIsIl9pbnRlcm9wUmVxdWlyZURlZmF1bHQiLCJfZXZlbnRzIiwiX3V0aWwiLCJlIiwiX19lc01vZHVsZSIsImRlZmF1bHQiLCJQYXJzZVdlYlNvY2tldFNlcnZlciIsImNvbnN0cnVjdG9yIiwic2VydmVyIiwib25Db25uZWN0IiwiY29uZmlnIiwid3NzIiwibG9hZEFkYXB0ZXIiLCJ3c3NBZGFwdGVyIiwiV1NBZGFwdGVyIiwib25MaXN0ZW4iLCJsb2dnZXIiLCJpbmZvIiwib25Db25uZWN0aW9uIiwid3MiLCJ3YWl0aW5nRm9yUG9uZyIsIm9uIiwiZXJyb3IiLCJtZXNzYWdlIiwiaW5zcGVjdCIsIlBhcnNlV2ViU29ja2V0IiwicGluZ0ludGVydmFsSWQiLCJzZXRJbnRlcnZhbCIsInBpbmciLCJjbGVhckludGVydmFsIiwidGVybWluYXRlIiwid2Vic29ja2V0VGltZW91dCIsIm9uRXJyb3IiLCJzdGFydCIsImNsb3NlIiwiZXhwb3J0cyIsImV2ZW50cyIsIkV2ZW50RW1pdHRlciIsIm9ubWVzc2FnZSIsInJlcXVlc3QiLCJlbWl0IiwiZGF0YSIsIm9uY2xvc2UiLCJzZW5kIl0sInNvdXJjZXMiOlsiLi4vLi4vc3JjL0xpdmVRdWVyeS9QYXJzZVdlYlNvY2tldFNlcnZlci5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBsb2FkQWRhcHRlciB9IGZyb20gJy4uL0FkYXB0ZXJzL0FkYXB0ZXJMb2FkZXInO1xuaW1wb3J0IHsgV1NBZGFwdGVyIH0gZnJvbSAnLi4vQWRhcHRlcnMvV2ViU29ja2V0U2Vydm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