SockJSClient.js 2.0 KB

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