Skip to content

Commit 3d767cc

Browse files
authored
Merge pull request #7 from CeeblueTV/dev
minor fix and feature update
2 parents 4f2cd45 + 4c73b25 commit 3d767cc

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/Util.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ export function objectEntries(value: any): [string, any][] {
142142
* @param obj Any objects, strings, exceptions, errors, or number
143143
* @param params.space `''`, allows to configure space in the string representation
144144
* @param params.decimal `2`, allows to choose the number of decimal to display in the string representation
145-
* @param params.recursive `false`, allows to serialize recursively every object value, beware if a value refers to a already parsed value an infinite loop will occur
145+
* @param params.recursion `1`, recursion depth to stringify recursively every object value until this depth, beware if a value refers to a already parsed value an infinite loop will occur
146146
* @param params.noBin `false`, when set skip binary encoding and write inplace a bin-length information
147147
* @returns the final string representation
148148
*/
149149
// Online Javascript Editor for free
150150
// Write, Edit and Run your Javascript code using JS Online Compiler
151151
export function stringify(
152152
obj: any,
153-
params: { space?: string; decimal?: number; recursive?: number; noBin?: boolean } = {}
153+
params: { space?: string; decimal?: number; recursion?: number; noBin?: boolean } = {}
154154
): string {
155-
params = Object.assign({ space: ' ', decimal: 2, recursive: 1, noBin: false }, params);
155+
params = Object.assign({ space: ' ', decimal: 2, recursion: 1, noBin: false }, params);
156156
if (obj == null) {
157157
return String(obj);
158158
}
@@ -173,7 +173,7 @@ export function stringify(
173173
return '[' + obj.byteLength + '#bytes]';
174174
}
175175
// boolean or string type or stop recursivity
176-
if (typeof obj === 'boolean' || obj.substring || !params.recursive) {
176+
if (typeof obj === 'boolean' || obj.substring || !params.recursion) {
177177
// is already a string OR has to be stringified
178178
return String(obj);
179179
}
@@ -185,14 +185,14 @@ export function stringify(
185185
let res = '';
186186
for (const value of obj) {
187187
res += (res ? ',' : '[') + space;
188-
res += stringify(value, Object.assign({ ...params }, { recursive: params.recursive - 1 }));
188+
res += stringify(value, Object.assign({ ...params }, { recursion: params.recursion - 1 }));
189189
}
190190
return (res += space + ']');
191191
}
192192
let res = '';
193193
for (const name in obj) {
194194
res += (res ? ',' : '{') + space + name + ':';
195-
res += stringify(obj[name], Object.assign({ ...params }, { recursive: params.recursive - 1 }));
195+
res += stringify(obj[name], Object.assign({ ...params }, { recursion: params.recursion - 1 }));
196196
}
197197
return (res += space + '}');
198198
}

src/WebSocketReliable.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,15 @@ export class WebSocketReliable extends EventEmitter {
168168
ws.onclose = (e: CloseEvent) => {
169169
if (!this._opened) {
170170
// close during connection
171-
this.close(url.toString() + ' connection failed (' + String(e.reason || e.code) + ')');
171+
// the caller can differentiate this case of one server shutdown by looking if onOpen was op
172+
this.close(url.toString() + ' connection failed, ' + String(e.reason || e.code));
172173
} else if (e.code === 1000 || e.code === 1005) {
173-
// normal disconnection from server (no error code)
174+
// normal disconnection from server, no error to indicate that a reconnection is possible!
175+
// the caller can differentiate this case of one explicit websocket.close() call in the encpasulating class
174176
this.close(url.toString() + ' shutdown');
175177
} else {
176-
// disconnection from server
177-
this.close(url.toString() + ' disconnection (' + String(e.reason || e.code) + ')');
178+
// abnormal disconnection from server
179+
this.close(url.toString() + ' disconnection, ' + String(e.reason || e.code));
178180
}
179181
};
180182
// Wrap send method to queue messages until connection is established.
@@ -193,7 +195,7 @@ export class WebSocketReliable extends EventEmitter {
193195
* @returns this
194196
*/
195197
send(message: string | ArrayBuffer | ArrayBufferView, queueing: boolean = false) {
196-
if (!this._ws) {
198+
if (this._closed) {
197199
throw Error('Open socket before to send data');
198200
}
199201
if (queueing || !this._opened) {
@@ -230,10 +232,13 @@ export class WebSocketReliable extends EventEmitter {
230232
this._ws.onopen = this._ws.onclose = this._ws.onmessage = null; // otherwise can receive message AFTER close!
231233
this._ws.close(); // Don't set to undefined to keep this._ws properties valid!
232234
// release resources!
233-
this._opened = false;
235+
234236
this._queueing.length = 0;
235237
this._queueingBytes = 0;
236238
this.onClose(error);
239+
// Reset _opened in last to allow to differenciate in onClose an error while connecting OR while connected
240+
// Is welcome to attempt a reconnection when no error OR when error on connection!
241+
this._opened = false;
237242
}
238243

239244
private _send(message: string | ArrayBuffer | ArrayBufferView) {

0 commit comments

Comments
 (0)