SockJSClient.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 SockJS from "../modules/sockjs-client/index.js";
  8. import { log } from "../utils/log.js";
  9. /** @typedef {import("../index").EXPECTED_ANY} EXPECTED_ANY */
  10. /**
  11. * @implements {CommunicationClient}
  12. */
  13. var SockJSClient = /*#__PURE__*/function () {
  14. /**
  15. * @param {string} url url
  16. */
  17. function SockJSClient(url) {
  18. _classCallCheck(this, SockJSClient);
  19. // SockJS requires `http` and `https` protocols
  20. this.sock = new SockJS(url.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:"));
  21. this.sock.onerror = function (error) {
  22. log.error(error);
  23. };
  24. }
  25. /**
  26. * @param {(...args: EXPECTED_ANY[]) => void} fn function
  27. */
  28. return _createClass(SockJSClient, [{
  29. key: "onOpen",
  30. value: function onOpen(fn) {
  31. this.sock.onopen = fn;
  32. }
  33. /**
  34. * @param {(...args: EXPECTED_ANY[]) => void} fn function
  35. */
  36. }, {
  37. key: "onClose",
  38. value: function onClose(fn) {
  39. this.sock.onclose = fn;
  40. }
  41. // call f with the message string as the first argument
  42. /**
  43. * @param {(...args: EXPECTED_ANY[]) => void} fn function
  44. */
  45. }, {
  46. key: "onMessage",
  47. value: function onMessage(fn) {
  48. this.sock.onmessage = function (err) {
  49. fn(err.data);
  50. };
  51. }
  52. }]);
  53. }();
  54. export { SockJSClient as default };