WebSocketClient.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 { log } from "../utils/log.js";
  7. var WebSocketClient = /*#__PURE__*/function () {
  8. /**
  9. * @param {string} url
  10. */
  11. function WebSocketClient(url) {
  12. _classCallCheck(this, WebSocketClient);
  13. this.client = new WebSocket(url);
  14. this.client.onerror = function (error) {
  15. log.error(error);
  16. };
  17. }
  18. /**
  19. * @param {(...args: any[]) => void} f
  20. */
  21. _createClass(WebSocketClient, [{
  22. key: "onOpen",
  23. value: function onOpen(f) {
  24. this.client.onopen = f;
  25. }
  26. /**
  27. * @param {(...args: any[]) => void} f
  28. */
  29. }, {
  30. key: "onClose",
  31. value: function onClose(f) {
  32. this.client.onclose = f;
  33. }
  34. // call f with the message string as the first argument
  35. /**
  36. * @param {(...args: any[]) => void} f
  37. */
  38. }, {
  39. key: "onMessage",
  40. value: function onMessage(f) {
  41. this.client.onmessage = function (e) {
  42. f(e.data);
  43. };
  44. }
  45. }]);
  46. return WebSocketClient;
  47. }();
  48. export { WebSocketClient as default };