Skip to content

Commit

Permalink
Add onConnect and onDisconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed Dec 14, 2024
1 parent 3200d95 commit 99550fe
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/utils/hydra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class Hydra {
constructor(
url: string | URL,
filterAddress?: string,
public onConnect?: () => void,
public onDisconnect?: () => void,
public queue_length: number = 10,
) {
this.tx_count = 0;
Expand All @@ -69,19 +71,23 @@ export class Hydra {
this.url.protocol = this.url.protocol.replace("ws", "http");
const websocketUrl = new URL(url);
websocketUrl.protocol = websocketUrl.protocol.replace("http", "ws");

let self = this;
this.connection = new WebSocket(
websocketUrl +
(websocketUrl.toString().endsWith("/") ? "" : "/") +
`?${filterAddress ? `address=${filterAddress}&` : ""}history=no`,
);
this.connection.onopen = () => {
console.log("Connected to Hydra");
self.onConnect?.();
};
this.connection.onerror = (error) => {
console.error("Error on Hydra websocket: ", error);
};
this.connection.onclose = () => {
console.error("Hydra websocket closed");
self.onDisconnect?.();
};
this.connection.onmessage = this.receiveMessage.bind(this);
}
Expand Down

0 comments on commit 99550fe

Please sign in to comment.