Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes in ws.send and address #24

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions base/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"name": "@bhoos/websocket-base",
"version": "0.1.1",
"version": "0.2.0",
"description": "reliable websocket client",
"repository": "https://github.com/Bhoos/websocket",
"author": "Bibek Panthi <bpanthi977@gmail.com>",
"license": "MIT",
"main": "es6/index.js",
"main": "dist/index.js",
"module": "es6/index.js",
"type": "module",
"types": "src/index.ts",
"react-native": "src/index.ts",
"files": [
"es6",
"dist"
"dist",
"src"
],
"scripts": {
"build:cjs": "tsc",
Expand Down
8 changes: 5 additions & 3 deletions base/src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export enum BufferType {

export interface Buffer <T> {
size : number; // Total number of items that can be added to the buffer.
add(...items : T[]) : void; // Add an item. Maynot be added if there's no space, as per the rules of the buffer.
add(...items : T[]) : boolean; // Add items. Maynot be added if there's no space, as per the rules of the buffer.
clear() : void; // Remove all items from buffer
forEach(fn:(el:T) => void) :void; // Loop through all existing items. Earlier ones first.
toArray(): T[]; // Return an array of all existing items with earlier ones in the beginning of array.
Expand Down Expand Up @@ -42,8 +42,9 @@ export class RingBuffer<T> implements Buffer<T> {
this.count = Math.min(this.count + 1 , this.size);
}

add(...items : T[]) : void{
add(...items : T[]): boolean {
items.forEach(this.addItem.bind(this));
return this.size >= items.length;
}

clear(): void {
Expand Down Expand Up @@ -85,14 +86,15 @@ export class FixedQueueBuffer<T> implements Buffer<T>{
this.buffer = Array(size);
}

add(...items : T[]) : void {
add(...items : T[]) : boolean {
// Add items to buffer (starting from first) until the buffer is full.
const freeSize = this.size - this.count;
const len = Math.min(items.length, freeSize);
for (var i = 0; i < len; i++) {
this.buffer[this.count] = items[i];
this.count++;
}
return freeSize >= items.length;
}

clear(): void {
Expand Down
18 changes: 14 additions & 4 deletions base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ReliableWS<
WSArgs
> {
private ws?: WebSocket;
private address: string | (() => string);
private address: string | (() => string | null);
private config: Config;

private wsOpen: boolean = false;
Expand All @@ -62,7 +62,7 @@ export class ReliableWS<
ondisconnect: ((event: CloseEv, tries: number) => void) | null = null;
onreconnect: ((event: Ev) => void) | null = null;

constructor(address: string | (() => string), options: Config, wsargs?: WSArgs) {
constructor(address: string | (() => string | null), options: Config, wsargs?: WSArgs) {
this.address = address;
this.config = options;
this.wsargs = wsargs;
Expand All @@ -78,6 +78,14 @@ export class ReliableWS<
address = this.address
else
address = this.address();

if (address === null) {
this.reconnectTimeout = setTimeout(
this.setupConnection.bind(this),
this.getReconnectionInterval(),
);
return;
}
//@ts-ignore
this.ws = new WebSocket(address, this.wsargs);

Expand Down Expand Up @@ -168,11 +176,12 @@ export class ReliableWS<
: this.config.RECONNECT_INTERVAL(this.tries);
}

send(msg: any) {
send(msg: any): boolean {
if (this.wsOpen && this.ws) {
this.ws.send(msg);
return true;
} else {
this.msgBuffer.add(msg);
return this.msgBuffer.add(msg);
}
}

Expand All @@ -185,5 +194,6 @@ export class ReliableWS<
if (this.pingTimer) this.clearPingTimer();
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
if (this.ws && this.wsOpen) this.ws.close();
this.msgBuffer.clear();
}
}
4 changes: 2 additions & 2 deletions browser/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@bhoos/websocket",
"version": "0.1.1",
"version": "0.2.0",
"main": "es6/index.js",
"license": "MIT",
"dependencies": {
"@bhoos/websocket-base": "^0.1.1"
"@bhoos/websocket-base": "^0.2.0"
},
"type": "module",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@bhoos/websocket-node",
"version": "0.1.1",
"version": "0.2.0",
"main": "es6/index.js",
"license": "MIT",
"dependencies": {
"@bhoos/websocket-base": "^0.1.1",
"@bhoos/websocket-base": "^0.2.0",
"ws": "^8.8.1"
},
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@bhoos/websocket",
"name": "@bhoos/websocket-repo",
"private": true,
"workspaces": [
"base",
Expand Down
Loading