UNPKG

2.01 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _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); } }
4
5function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
6
7import SockJS from "../modules/sockjs-client/index.js";
8import { log } from "../utils/log.js";
9
10var SockJSClient = /*#__PURE__*/function () {
11 /**
12 * @param {string} url
13 */
14 function SockJSClient(url) {
15 _classCallCheck(this, SockJSClient);
16
17 // SockJS requires `http` and `https` protocols
18 this.sock = new SockJS(url.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:"));
19
20 this.sock.onerror =
21 /**
22 * @param {Error} error
23 */
24 function (error) {
25 log.error(error);
26 };
27 }
28 /**
29 * @param {(...args: any[]) => void} f
30 */
31
32
33 _createClass(SockJSClient, [{
34 key: "onOpen",
35 value: function onOpen(f) {
36 this.sock.onopen = f;
37 }
38 /**
39 * @param {(...args: any[]) => void} f
40 */
41
42 }, {
43 key: "onClose",
44 value: function onClose(f) {
45 this.sock.onclose = f;
46 } // call f with the message string as the first argument
47
48 /**
49 * @param {(...args: any[]) => void} f
50 */
51
52 }, {
53 key: "onMessage",
54 value: function onMessage(f) {
55 this.sock.onmessage =
56 /**
57 * @param {Error & { data: string }} e
58 */
59 function (e) {
60 f(e.data);
61 };
62 }
63 }]);
64
65 return SockJSClient;
66}();
67
68export { SockJSClient as default };
\No newline at end of file