Skip to content

Commit c3ad39c

Browse files
committed
update documentation
1 parent 66b4a35 commit c3ad39c

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
# forever-websocket
2+
WebSocket client, reconnecting and isomorphic, a simple implementation
3+
4+
## Features
5+
* WebSocket API compatible.
6+
7+
It exposes all properties and methods. API documentation is still valid: [MDN WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) for web browser and [Node.js WebSocket](https://github.com/websockets/ws/blob/master/doc/ws.md) for node.js
8+
* Reconnecting, if connection drops
9+
* Configurable reconnecting timers
10+
* Configurable timeouts and reconnects when no message received
11+
* Configurable pings to keep connection alive
12+
* Allows changing URL and parameters between reconnections
13+

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
{
22
"name": "forever-websocket",
33
"version": "0.1.0",
4-
"description": "Reconnecting and isomorphic WebSocket client - a simple implementation",
4+
"description": "WebSocket client, reconnecting and isomorphic, a simple implementation",
55
"main": "src/index.mjs",
66
"module": "src/index.mjs",
77
"repository": {
88
"type": "git",
9-
"url": "https://github.com/dutu/xworker-polo-3trad"
9+
"url": "https://github.com/dutu/forever-websocket"
1010
},
1111
"private": true,
1212
"keywords": [
1313
"WebSocket",
1414
"ws",
1515
"reconnecting",
1616
"reconnect",
17-
"persistent"
17+
"persistent",
18+
"isomorphic"
1819
],
1920
"packageManager": "yarn@3.4.1",
2021
"dependencies": {

src/index.mjs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@ import ws from 'ws'
22
import EventEmitter from 'eventemitter3'
33

44
/**
5-
* This class represents a reconnecting WebSocket. It extends the EventEmitter.
5+
* This class represents a reconnecting WebSocket client. It extends the EventEmitter.
66
*
7-
* The class exposes all websocket
7+
* The class exposes all WebSocket properties and methods
88
* WebSocket client https://github.com/websockets/ws
99
*/
10-
11-
// Names of properties which are not cloned from underlying WebSocket
12-
const ownEventNames = ['connecting', 'delay', 'timeout', 'newListener', 'removeListener', 'reconnected']
13-
10+
export class ForeverWebSocket extends EventEmitter {
11+
// Names of properties which are not cloned from underlying WebSocket
12+
#ownEventNames = ['connecting', 'delay', 'timeout', 'newListener', 'removeListener', 'reconnected']
1413
// Property names for `options`
15-
const optionsPropertyNames = ['automaticOpen', 'reconnect', 'timeout', 'ping', 'newWebsocketFn']
16-
17-
// Detects if the property name is a method of obj
18-
function isMethod (obj, propertyName) {
19-
const desc = Object.getOwnPropertyDescriptor (obj, propertyName);
20-
return !!desc && typeof desc.value === 'function';
21-
}
14+
#optionsPropertyNames = ['automaticOpen', 'reconnect', 'timeout', 'ping', 'newWebsocketFn']
2215

23-
export class ReconnectingWebSocketClient extends EventEmitter {
2416
/**
2517
*
2618
* @param {string} address - The URL to which to connect
@@ -42,6 +34,12 @@ export class ReconnectingWebSocketClient extends EventEmitter {
4234
constructor(address, protocol, options) {
4335
super()
4436

37+
// Detects if the property name is a method of obj
38+
const isMethod = (obj, propertyName) => {
39+
const desc = Object.getOwnPropertyDescriptor (obj, propertyName);
40+
return !!desc && typeof desc.value === 'function';
41+
}
42+
4543
// Store parameters
4644
this._address = address
4745
if (Array.isArray(protocol) || typeof protocol === 'string') {
@@ -54,7 +52,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
5452

5553
this._optionsWebSocket = {}
5654
Object.keys(this._options).forEach((key) => {
57-
if (!optionsPropertyNames.includes(key)) {
55+
if (!this.#optionsPropertyNames.includes(key)) {
5856
this._optionsWebSocket[key] = this._options[key]
5957
}
6058
})
@@ -262,7 +260,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
262260
* @param options
263261
*/
264262
on(eventName, listener, options) {
265-
if (ownEventNames.includes(eventName)) {
263+
if (this.#ownEventNames.includes(eventName)) {
266264
return super.on(eventName, listener)
267265
}
268266

@@ -302,7 +300,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
302300
* Alias for `on`
303301
*/
304302
addEventListener(eventName, listener, options) {
305-
if (ownEventNames.includes(eventName)) {
303+
if (this.#ownEventNames.includes(eventName)) {
306304
return super.on(eventName, listener)
307305
}
308306

@@ -327,9 +325,8 @@ export class ReconnectingWebSocketClient extends EventEmitter {
327325
return this
328326
}
329327

330-
331328
off(eventName, listener) {
332-
if (ownEventNames.includes(eventName)) {
329+
if (this.#ownEventNames.includes(eventName)) {
333330
return super.removeListener(eventName, listener)
334331
}
335332

@@ -350,7 +347,6 @@ export class ReconnectingWebSocketClient extends EventEmitter {
350347
this.addListener(...args)
351348
}
352349

353-
354350
/**
355351
* Returns the readyState of the underlying WebSocket or `undefined` if it does not exist.
356352
* When the underlying WebSocket object does not exist it returns `undefined`
@@ -373,7 +369,6 @@ export class ReconnectingWebSocketClient extends EventEmitter {
373369
this.ws = new ws(this._address, this._protocol, this._optionsWebSocket)
374370
}
375371

376-
377372
// When WebSocket connection is open, restart ping and timeout factories, and reset the reconnect factory
378373
this.ws.addEventListener('open', () => {
379374
this._pingFactory?.start()
@@ -441,7 +436,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
441436
*
442437
* Sends data to the WebsocketServer.
443438
*
444-
* The method extends `WebSocket.send()` method, so that and `Object` can be passed, in which case it is stringfied before sending.
439+
* The method extends `WebSocket.send()` method, so that and `Object` can be passed. In this case the object is stringfied before sending.
445440
*
446441
* It will throw an exception if you call send() when the connection is in the CONNECTING state or when underlying WebSocket object does not exist.
447442
*
@@ -491,5 +486,3 @@ export class ReconnectingWebSocketClient extends EventEmitter {
491486
}
492487
}
493488
}
494-
495-

0 commit comments

Comments
 (0)