SockJSClient.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  3. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  4. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
  5. function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  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. _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. return SockJSClient;
  57. }();
  58. export { SockJSClient as default };