WebsocketClient.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. /* global WebSocket */
  3. /* eslint-disable
  4. no-unused-vars
  5. */
  6. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  10. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  11. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  12. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  13. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  14. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  15. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  16. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  17. var BaseClient = require('./BaseClient');
  18. module.exports = /*#__PURE__*/function (_BaseClient) {
  19. _inherits(WebsocketClient, _BaseClient);
  20. var _super = _createSuper(WebsocketClient);
  21. function WebsocketClient(url) {
  22. var _this;
  23. _classCallCheck(this, WebsocketClient);
  24. _this = _super.call(this);
  25. _this.client = new WebSocket(url.replace(/^http/, 'ws'));
  26. _this.client.onerror = function (err) {// TODO: use logger to log the error event once client and client-src
  27. // are reorganized to have the same directory structure
  28. };
  29. return _this;
  30. }
  31. _createClass(WebsocketClient, [{
  32. key: "onOpen",
  33. value: function onOpen(f) {
  34. this.client.onopen = f;
  35. }
  36. }, {
  37. key: "onClose",
  38. value: function onClose(f) {
  39. this.client.onclose = f;
  40. } // call f with the message string as the first argument
  41. }, {
  42. key: "onMessage",
  43. value: function onMessage(f) {
  44. this.client.onmessage = function (e) {
  45. f(e.data);
  46. };
  47. }
  48. }], [{
  49. key: "getClientPath",
  50. value: function getClientPath(options) {
  51. return require.resolve('./WebsocketClient');
  52. }
  53. }]);
  54. return WebsocketClient;
  55. }(BaseClient);