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

Add new features #25

Merged
merged 4 commits into from
Nov 28, 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
13 changes: 8 additions & 5 deletions base/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"name": "@bhoos/websocket-base",
"version": "0.2.0",
"version": "1.0.0",
"description": "reliable websocket client",
"repository": "https://github.com/Bhoos/websocket",
"author": "Bibek Panthi <bpanthi977@gmail.com>",
"license": "MIT",
"main": "dist/index.js",
"module": "es6/index.js",
"type": "module",
"types": "src/index.ts",
"react-native": "src/index.ts",
"exports": {
".": {
"import": "./es6/index.js",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"files": [
"es6",
"dist",
Expand All @@ -23,8 +29,5 @@
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"devDependencies": {
"typescript": "^4.1.3"
}
}
46 changes: 20 additions & 26 deletions base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@ export type Config = {
};

interface Event {
type: string;
}

interface ErrorEvent extends Event {
}

interface CloseEvent extends Event {
wasClean: boolean;
code: number;
reason: string;
}

interface MessageEvent extends Event {
data: any;
}

// WSAgent provides a reliable websocket connection
// it sends heartbeats (ping), reconnects if connection is broken
// and also keeps a buffer for messages that were send while the connection is down
// and those messages are later sent when connection is up
export class ReliableWS<
Ev extends Event,
ErrorEv extends ErrorEvent,
CloseEv extends CloseEvent,
MessageEv extends MessageEvent,
ErrorEv extends Event,
CloseEv extends Event,
MessageEv extends Event,
WSArgs
> {
private ws?: WebSocket;
Expand All @@ -52,15 +37,15 @@ export class ReliableWS<
private wsargs?: WSArgs;
private tries: number = 0;

onopen: ((event: Ev) => void) | null = null;
onopen: (() => void) | null = null;
onerror: ((event: ErrorEv) => void) | null = null;
onclose: ((event: CloseEv) => void) | null = null;
onmessage: ((event: MessageEv) => void) | null = null;
// this event is issued when the connection is disconnected
// reliable websocket will nonethless be trying connection attempts.
// tries: number of times connection has been tried to establish since start or after the last successfull connection
ondisconnect: ((event: CloseEv, tries: number) => void) | null = null;
onreconnect: ((event: Ev) => void) | null = null;
onreconnect: (() => void) | null = null;

constructor(address: string | (() => string | null), options: Config, wsargs?: WSArgs) {
this.address = address;
Expand Down Expand Up @@ -94,15 +79,14 @@ export class ReliableWS<
if (this.onerror) this.onerror(event);
};

this.ws.onopen = _event => {
const event = _event as unknown as Ev;
this.ws.onopen = () => {
this.wsOpen = true;
this.tries = 0;
if (this.onopen && !this.onceOpened) {
this.onceOpened = true;
this.onopen(event); // this is triggerred on the first time only
this.onopen(); // this is triggerred on the first time only
} else if (this.onreconnect && this.onceOpened) {
this.onreconnect(event);
this.onreconnect();
}

if (this.ws)
Expand Down Expand Up @@ -133,6 +117,7 @@ export class ReliableWS<
}
// this means connection was closed unexpetedly
this.wsOpen = false;
this.ws = undefined;
this.clearPingTimer();
this.reconnectTimeout = setTimeout(
this.setupConnection.bind(this),
Expand All @@ -141,6 +126,13 @@ export class ReliableWS<
};
}

tryConnection() {
if (this.shuttingDown) return false;
if (this.wsOpen || this.ws) return false;
clearTimeout(this.reconnectTimeout)
this.setupConnection();
}

changeConfig(config: Config) {
this.config = config;
if (this.pingTimer) { // if connection is ok and ping is running, reset its timer.
Expand Down Expand Up @@ -176,12 +168,14 @@ export class ReliableWS<
: this.config.RECONNECT_INTERVAL(this.tries);
}

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

Expand Down
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.2.0",
"version": "1.0.0",
"main": "es6/index.js",
"license": "MIT",
"dependencies": {
"@bhoos/websocket-base": "^0.2.0"
"@bhoos/websocket-base": "^1.0.0"
},
"type": "module",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReliableWS as WS } from '@bhoos/websocket-base'
export { Config, BufferType } from '@bhoos/websocket-base'
export class ReliableWS extends WS<Event, Event, CloseEvent, MessageEvent, string | string[]> {
export class ReliableWS extends WS<Event, CloseEvent, MessageEvent, string | string[]> {
}
4 changes: 2 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"node"
],
"npmClient": "yarn",
"version": "0.1.1",
"version": "0.3.0",
"useWorkspaces": true
}
}
6 changes: 3 additions & 3 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@bhoos/websocket-node",
"version": "0.2.0",
"version": "1.0.0",
"main": "es6/index.js",
"license": "MIT",
"dependencies": {
"@bhoos/websocket-base": "^0.2.0",
"@bhoos/websocket-base": "^1.0.0",
"ws": "^8.8.1"
},
"type": "module",
Expand All @@ -22,6 +22,6 @@
"registry": "https://npm.pkg.github.com/"
},
"devDependencies": {
"@types/ws": "^8.5.3"
"@types/ws": "^8.0.0"
}
}
17 changes: 13 additions & 4 deletions node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import WebSocket from 'ws';
import { ClientOptions, Event, CloseEvent, MessageEvent, ErrorEvent} from 'ws';
import { ClientOptions, CloseEvent, MessageEvent, ErrorEvent} from 'ws';
import { ClientRequestArgs } from 'http';
import { ReliableWS as WS, BufferType } from '@bhoos/websocket-base'
//@ts-ignore
global.WebSocket = WebSocket
export class ReliableWS extends WS<Event, ErrorEvent, CloseEvent, MessageEvent, ClientOptions | ClientRequestArgs> {

type NodeWS = WebSocket
declare global {
type WebSocket = NodeWS
var WebSocket: new (
address: string | URL,
protocols?: string | string[],
options?: WebSocket.ClientOptions | ClientRequestArgs,
) => WebSocket
}

export class ReliableWS extends WS<ErrorEvent, CloseEvent, MessageEvent, ClientOptions | ClientRequestArgs> {
}
export {BufferType}
1 change: 1 addition & 0 deletions node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"strict": true,
"target": "es2017",
"lib": ["es2017"],
"types": ["node"],
"module": "commonjs",
"esModuleInterop": true,
"noImplicitAny": true,
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"private": true,
"workspaces": [
"base",
"node",
"browser"
"browser",
"node"
],
"scripts": {
"build": "lerna run build",
"release": "lerna run release"
},
"devDependencies": {
"lerna": "^4.0.0"
"lerna": "^4.0.0",
"typescript": "^4.1.3"
}
}
Loading
Loading