@centia-io/sdk 0.0.53 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/centia-io-sdk.cjs +84 -25
- package/dist/centia-io-sdk.d.cts +65 -7
- package/dist/centia-io-sdk.d.cts.map +1 -1
- package/dist/centia-io-sdk.d.ts +65 -7
- package/dist/centia-io-sdk.d.ts.map +1 -1
- package/dist/centia-io-sdk.js +84 -25
- package/dist/centia-io-sdk.js.map +1 -1
- package/dist/centia-io-sdk.umd.js +84 -25
- package/package.json +1 -1
|
@@ -741,35 +741,94 @@
|
|
|
741
741
|
*/
|
|
742
742
|
var Ws = class {
|
|
743
743
|
constructor(options) {
|
|
744
|
-
this.
|
|
745
|
-
this.
|
|
744
|
+
this.ws = null;
|
|
745
|
+
this.listeners = {};
|
|
746
|
+
this.closed = false;
|
|
747
|
+
this.options = {
|
|
748
|
+
reconnect: true,
|
|
749
|
+
reconnectInterval: 3e3,
|
|
750
|
+
...options
|
|
751
|
+
};
|
|
752
|
+
this.options.wsClient = this.options.wsClient ?? WebSocket;
|
|
746
753
|
}
|
|
747
754
|
connect() {
|
|
748
|
-
|
|
755
|
+
this.closed = false;
|
|
756
|
+
this.doConnect();
|
|
757
|
+
}
|
|
758
|
+
disconnect() {
|
|
759
|
+
this.closed = true;
|
|
760
|
+
this.ws?.close();
|
|
761
|
+
this.ws = null;
|
|
762
|
+
}
|
|
763
|
+
subscribe(sub) {
|
|
764
|
+
this.send({
|
|
765
|
+
type: "subscription",
|
|
766
|
+
...sub
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
send(data) {
|
|
770
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error("WebSocket is not connected");
|
|
771
|
+
this.ws.send(typeof data === "string" ? data : JSON.stringify(data));
|
|
772
|
+
}
|
|
773
|
+
on(event, listener) {
|
|
774
|
+
if (!this.listeners[event]) this.listeners[event] = [];
|
|
775
|
+
this.listeners[event].push(listener);
|
|
776
|
+
return () => this.off(event, listener);
|
|
777
|
+
}
|
|
778
|
+
off(event, listener) {
|
|
779
|
+
const arr = this.listeners[event];
|
|
780
|
+
if (!arr) return;
|
|
781
|
+
const idx = arr.indexOf(listener);
|
|
782
|
+
if (idx !== -1) arr.splice(idx, 1);
|
|
783
|
+
}
|
|
784
|
+
get connected() {
|
|
785
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
786
|
+
}
|
|
787
|
+
emit(event, data) {
|
|
788
|
+
const arr = this.listeners[event];
|
|
789
|
+
if (!arr) return;
|
|
790
|
+
for (const fn of arr) fn(data);
|
|
791
|
+
}
|
|
792
|
+
doConnect() {
|
|
749
793
|
const { accessToken } = getTokens();
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
794
|
+
if (!accessToken) return;
|
|
795
|
+
let url = this.options.host + "/?token=" + encodeURIComponent(accessToken);
|
|
796
|
+
if (this.options.rels) url += "&rels=" + encodeURIComponent(this.options.rels);
|
|
797
|
+
const WSClass = this.options.wsClient;
|
|
798
|
+
const ws = new WSClass(url);
|
|
799
|
+
this.ws = ws;
|
|
800
|
+
ws.onopen = () => {
|
|
801
|
+
this.emit("open", void 0);
|
|
802
|
+
};
|
|
803
|
+
ws.onmessage = (event) => {
|
|
804
|
+
let msg;
|
|
805
|
+
try {
|
|
806
|
+
msg = JSON.parse(typeof event.data === "string" ? event.data : event.data.toString());
|
|
807
|
+
} catch {
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
switch (msg.type) {
|
|
811
|
+
case "batch":
|
|
812
|
+
this.emit("batch", msg);
|
|
813
|
+
break;
|
|
814
|
+
case "subscription_ack":
|
|
815
|
+
this.emit("subscription_ack", msg);
|
|
816
|
+
break;
|
|
817
|
+
case "error":
|
|
818
|
+
this.emit("error", msg);
|
|
819
|
+
break;
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
ws.onclose = (event) => {
|
|
823
|
+
this.emit("close", {
|
|
824
|
+
code: event.code,
|
|
825
|
+
reason: event.reason
|
|
826
|
+
});
|
|
827
|
+
if (!this.closed && this.options.reconnect) setTimeout(() => this.doConnect(), this.options.reconnectInterval);
|
|
828
|
+
};
|
|
829
|
+
ws.onerror = () => {
|
|
830
|
+
ws.close();
|
|
771
831
|
};
|
|
772
|
-
if (accessToken !== "") connect();
|
|
773
832
|
}
|
|
774
833
|
};
|
|
775
834
|
|