WebSocketClient.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
  3. function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
  4. function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
  5. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  6. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  7. import { log } from "../utils/log.js";
  8. var WebSocketClient = /*#__PURE__*/function () {
  9. /**
  10. * @param {string} url
  11. */
  12. function WebSocketClient(url) {
  13. _classCallCheck(this, WebSocketClient);
  14. this.client = new WebSocket(url);
  15. this.client.onerror = function (error) {
  16. log.error(error);
  17. };
  18. }
  19. /**
  20. * @param {(...args: any[]) => void} f
  21. */
  22. return _createClass(WebSocketClient, [{
  23. key: "onOpen",
  24. value: function onOpen(f) {
  25. this.client.onopen = f;
  26. }
  27. /**
  28. * @param {(...args: any[]) => void} f
  29. */
  30. }, {
  31. key: "onClose",
  32. value: function onClose(f) {
  33. this.client.onclose = f;
  34. }
  35. // call f with the message string as the first argument
  36. /**
  37. * @param {(...args: any[]) => void} f
  38. */
  39. }, {
  40. key: "onMessage",
  41. value: function onMessage(f) {
  42. this.client.onmessage = function (e) {
  43. f(e.data);
  44. };
  45. }
  46. }]);
  47. }();
  48. export { WebSocketClient as default };