WebSocketClient.js 1.9 KB

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