WebSocketClient.js 1.7 KB

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