SockJSClient.js 2.4 KB

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