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

Reconnect on error #247

Merged
merged 1 commit into from
Jul 2, 2018
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
52 changes: 29 additions & 23 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@
clearTimeout(this._reconnectionTimer);
this._reconnectionTimer = null;

const reconnect = (event) => {

if (ws.onopen) {
finalize(new NesError('Connection terminated while waiting to connect', errorTypes.WS));
}

const wasRequested = this._disconnectRequested; // Get value before _cleanup()

this._cleanup();

const log = {
code: event.code,
explanation: errorCodes[event.code] || 'Unknown',
reason: event.reason,
wasClean: event.wasClean,
willReconnect: this._willReconnect(),
wasRequested
};

this.onDisconnect(log.willReconnect, log);
this._reconnect();
};

const finalize = (err) => {

if (next) { // Call only once when connect() is called
Expand Down Expand Up @@ -235,35 +258,18 @@
ws.onerror = (event) => {

clearTimeout(timeout);
this._cleanup();

const error = new NesError('Socket error', errorTypes.WS);
return finalize(error);
};

ws.onclose = (event) => {

if (ws.onopen) {
finalize(new NesError('Connection terminated while waiting to connect', errorTypes.WS));
if (this._willReconnect()){
return reconnect(event);
}

const wasRequested = this._disconnectRequested; // Get value before _cleanup()

this._cleanup();

const log = {
code: event.code,
explanation: errorCodes[event.code] || 'Unknown',
reason: event.reason,
wasClean: event.wasClean,
willReconnect: this._willReconnect(),
wasRequested
};

this.onDisconnect(log.willReconnect, log);
this._reconnect();
const error = new NesError('Socket error', errorTypes.WS);
return finalize(error);
};

ws.onclose = reconnect;

ws.onmessage = (message) => {

return this._onMessage(message);
Expand Down
8 changes: 4 additions & 4 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Client', () => {
team.attend();
};

await client.connect();
await client.connect({ reconnect: false });
client._ws.emit('error', new Error('test'));
await team.work;
});
Expand All @@ -56,7 +56,7 @@ describe('Client', () => {

const client = new Nes.Client('http://0');

const err = await expect(client.connect()).to.reject('Socket error');
const err = await expect(client.connect()).to.reject('Connection terminated while waiting to connect');
expect(err.type).to.equal('ws');
expect(err.isNes).to.equal(true);
client.disconnect();
Expand Down Expand Up @@ -530,7 +530,7 @@ describe('Client', () => {
client.onError = (err) => {

expect(err).to.exist();
expect(err.message).to.equal('Socket error');
expect(err.message).to.equal('Connection terminated while waiting to connect');
expect(err.type).to.equal('ws');
expect(err.isNes).to.equal(true);

Expand Down Expand Up @@ -562,7 +562,7 @@ describe('Client', () => {
}

expect(e).to.equal(1);
expect(r).to.equal('tttt');
expect(r).to.equal('ttttt');

team.attend();
};
Expand Down