SockJSClient.js 2.1 KB

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