Skip to content

Commit

Permalink
daemon.connect now accepts connection timeout in milliseconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaMineJP committed Aug 4, 2024
1 parent c9a6b3c commit c328aa6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [14.2.1]
### Changed
- `daemon.connect()` now returns a rejected promise on failure to connect instead of throwing an uncaught exception
- [`daemon.connect()`](./src/daemon/README.md#daemonconnecturl-string-timeoutms-number) now returns a rejected promise on failure to connect instead of throwing an uncaught exception
- [`daemon.connect()`](./src/daemon/README.md#daemonconnecturl-string-timeoutms-number) now accepts connection timeout in milliseconds.

## [14.2.0]
### Note
Expand Down
18 changes: 14 additions & 4 deletions src/daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const {getDaemon} = require("chia-agent");
const daemon = getDaemon();
```

## daemon.connect
## daemon.connect(url?: string, timeoutMs?: number)

Connect to chia daemon via websocket.

If you don't pass any argument, it tries to connect to an url specified in chia configuration file.
If you don't pass any argument, it tries to connect to an url specified in chia configuration file.
(Note: `chia-agent` cares environment variable `CHIA_ROOT`)
```js
await daemon.connect();
Expand All @@ -27,7 +27,17 @@ You can specify chia daemon url to connect to.
await daemon.connect("wss://hostname:port");
```

## daemon.close
### `url`
Optional. A URL to the daemon server to connect.
The default url is `wss://{DAEMON_HOST}:{DAEMON_PORT}` where
`DAEMON_HOST` is `ui.daemon_host` and `DAEMON_PORT` is `ui.daemon_port` in `$CHIA_ROOT/config/config.yaml`
(Usually `wss://localhost:55400`)

### `timeoutMs`
Optional. Connection timeout in milliseconds.
The default value is `50000`.

## daemon.close()

Close active connection.
If no active connection found, it silently returns without error.
Expand All @@ -42,7 +52,7 @@ daemon.addEventListener("close", () => {
});
```

## daemon.subscribe
## daemon.subscribe(service: string)

Start to monitor message channel via websocket.

Expand Down
1 change: 1 addition & 0 deletions src/daemon/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function open(url: string, timeoutMs?: number): Promise<{ ws: WS, openEve
resolve({ws, openEvent});
}
};

ws.onerror = (err) => reject(err);
});
}
5 changes: 3 additions & 2 deletions src/daemon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ class Daemon {
/**
* Connect to local daemon via websocket.
* @param daemonServerURL
* @param timeoutMs
*/
public async connect(daemonServerURL?: string): Promise<boolean> {
public async connect(daemonServerURL?: string, timeoutMs?: number): Promise<boolean> {
if(!daemonServerURL){
const config = getConfig();
const daemonHost = config["/ui/daemon_host"];
Expand All @@ -107,7 +108,7 @@ class Daemon {

getLogger().debug(`Opening websocket connection to ${daemonServerURL}`);

const result = await open(daemonServerURL);
const result = await open(daemonServerURL, timeoutMs);
this._socket = result.ws;
this._socket.onerror = this.onError;
this._socket.onmessage = this.onMessage;
Expand Down

0 comments on commit c328aa6

Please sign in to comment.