Skip to content

Commit

Permalink
Fix websocket default values
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Feb 29, 2024
1 parent c25c1c5 commit 648db68
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-bobcats-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ccat-api": patch
---

Fix websocket default values
15 changes: 8 additions & 7 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ export class CatClient {
}

private initWebSocket() {
const socketSettings = this.config.ws = {
const wsConfig = this.config.ws = {
delay: 3000,
path: 'ws',
path: '/ws',
retries: 3,
query: '',
...this.config.ws
} satisfies WebSocketSettings
const user = this.config.user ?? 'user'
this.ws = new WebSocket(`${this.url}/${socketSettings.path}/${user}${socketSettings.query}`)
this.ws = new WebSocket(`${this.url}${wsConfig.path}/${user}${wsConfig.query}`)
this.ws.onopen = () => {
this.connectedHandler?.()
}
this.ws.onclose = () => {
if (!this.explicitlyClosed) {
this.retried += 1
if (socketSettings.retries < 0 || this.retried < socketSettings.retries) {
setTimeout(() => this.initWebSocket(), socketSettings.delay)
} else socketSettings.onFailed?.({
if (wsConfig.retries < 0 || this.retried < wsConfig.retries) {
setTimeout(() => this.initWebSocket(), wsConfig.delay)
} else wsConfig.onFailed?.({
name: 'FailedRetry',
description: `Failed to connect WebSocket after ${socketSettings.retries} retries.`
description: `Failed to connect WebSocket after ${wsConfig.retries} retries.`
})
}
this.disconnectedHandler?.()
Expand Down
7 changes: 5 additions & 2 deletions api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export interface WebSocketSettings {
/**
* Websocket path to use to communicate with the CCat
* @default 'ws'
* @default '/ws'
*/
path?: string
/**
* The query to append to the URL. It should start with a question mark.
* @example '?token=123'
* @default ''
*/
query?: string
Expand All @@ -27,7 +28,9 @@ export interface WebSocketSettings {
}

export interface CatSettings {
/** The URL to which connect to the Cat */
/** The URL to which connect to the Cat
* @example 'localhost'
*/
baseUrl: string
/**
* The key to authenticate the Cat endpoints
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ declare class CCatAPI {
interface WebSocketSettings {
/**
* Websocket path to use to communicate with the CCat
* @default 'ws'
* @default '/ws'
*/
path?: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ declare class CCatAPI {
interface WebSocketSettings {
/**
* Websocket path to use to communicate with the CCat
* @default 'ws'
* @default '/ws'
*/
path?: string;
/**
Expand Down
5 changes: 3 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,13 @@ var CatClient = class {
initWebSocket() {
const socketSettings = this.config.ws = {
delay: 3e3,
path: "ws",
path: "/ws",
retries: 3,
query: "",
...this.config.ws
};
const user = this.config.user ?? "user";
this.ws = new import_isomorphic_ws.default(`${this.url}/${socketSettings.path}/${user}${socketSettings.query}`);
this.ws = new import_isomorphic_ws.default(`${this.url}${socketSettings.path}/${user}${socketSettings.query ?? ""}`);
this.ws.onopen = () => {
this.connectedHandler?.();
};
Expand Down
5 changes: 3 additions & 2 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,12 +1076,13 @@ var CatClient = class {
initWebSocket() {
const socketSettings = this.config.ws = {
delay: 3e3,
path: "ws",
path: "/ws",
retries: 3,
query: "",
...this.config.ws
};
const user = this.config.user ?? "user";
this.ws = new WebSocket(`${this.url}/${socketSettings.path}/${user}${socketSettings.query}`);
this.ws = new WebSocket(`${this.url}${socketSettings.path}/${user}${socketSettings.query ?? ""}`);
this.ws.onopen = () => {
this.connectedHandler?.();
};
Expand Down

0 comments on commit 648db68

Please sign in to comment.