@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.
@@ -741,35 +741,94 @@
741
741
  */
742
742
  var Ws = class {
743
743
  constructor(options) {
744
- this.options = options;
745
- this.options.wsClient = this.options?.wsClient ?? WebSocket;
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
- const me = this;
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
- const connect = () => {
751
- let queryString = `?token=` + accessToken;
752
- if (this.options?.rel) queryString = queryString + `&rel=` + this.options.rel;
753
- const WSClass = this.options.wsClient;
754
- const ws = new WSClass(this.options.host + `/` + queryString);
755
- ws.onopen = function() {
756
- console.log("WebSocket connected!");
757
- };
758
- ws.onmessage = function(event) {
759
- me.options.callBack(event.data);
760
- };
761
- ws.onclose = function(event) {
762
- if (accessToken !== "") {
763
- console.log("WebSocket closed, reconnecting in 3 seconds...", event.reason);
764
- setTimeout(connect, 3e3);
765
- }
766
- };
767
- ws.onerror = function(err) {
768
- console.error("WebSocket error observed:", err);
769
- ws.close();
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centia-io/sdk",
3
- "version": "0.0.53",
3
+ "version": "0.0.54",
4
4
  "description": "Centia-io TypeScript SDK",
5
5
  "author": "Martin Høgh",
6
6
  "license": "MIT",