Skip to content
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
2 changes: 1 addition & 1 deletion src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Kuzzle.prototype.connect = function () {
});

self.network.onConnectError(function (error) {
var connectionError = new Error('Unable to connect to kuzzle server at "' + self.host + '"');
var connectionError = new Error('Unable to connect to kuzzle proxy server at "' + self.host + '"');

connectionError.internal = error;
self.state = 'error';
Expand Down
41 changes: 30 additions & 11 deletions src/networkWrapper/wrappers/wsbrowsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,17 @@ function WSBrowsers(host, port, ssl) {
}
};

this.client.onclose = function () {
poke(self.listeners, 'disconnect');
};

this.client.onerror = function () {
if (autoReconnect) {
self.retrying = true;
setTimeout(function () {
self.connect(autoReconnect, reconnectionDelay);
}, reconnectionDelay);
this.client.onclose = function (code, message) {
if (code === 1000) {
poke(self.listeners, 'disconnect');
}
else {
onClientError.call(self, autoReconnect, reconnectionDelay, message);
}
};

poke(self.listeners, 'error');
this.client.onerror = function (error) {
onClientError.call(self, autoReconnect, reconnectionDelay, error);
};

this.client.onmessage = function (payload) {
Expand Down Expand Up @@ -234,4 +232,25 @@ function poke (listeners, roomId, payload) {
}
}

/**
* Called when the connection closes with an error state
*
* @param {boolean} autoReconnect
* @param {number} reconnectionDelay
* @param {string|Object} message
*/
function onClientError(autoReconnect, reconnectionDelay, message) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it possible to factorize a little bit the code btw. wsbrowsers.js and wsnode.js ?
(as far as I can see, only the code of this.connect() method has some differences in its syntax)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be, I separated the two wrappers because I initially thought that there would be more differences than that.

Right now the biggest problem is with webpack, when it tries to package ws and other modules that are incompatible with web browsers (crypto and bufferutil).
The only solution I found was to exclude wsnode.js from webpack's analysis. We need to find some way to factorize wsnode and wsbrowsers while preventing webpack to include these modules.

var self = this;

if (autoReconnect) {
self.retrying = true;
setTimeout(function () {
self.connect(autoReconnect, reconnectionDelay);
}, reconnectionDelay);
}

poke(self.listeners, 'error', message);
}


module.exports = WSBrowsers;
40 changes: 29 additions & 11 deletions src/networkWrapper/wrappers/wsnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,17 @@ function WSNode(host, port, ssl) {
}
});

this.client.on('close', function () {
poke(self.listeners, 'disconnect');
});

this.client.on('error', function () {
if (autoReconnect) {
self.retrying = true;
setTimeout(function () {
self.connect(autoReconnect, reconnectionDelay);
}, reconnectionDelay);
this.client.on('close', function (code, message) {
if (code === 1000) {
poke(self.listeners, 'disconnect');
}
else {
onClientError.call(self, autoReconnect, reconnectionDelay, message);
}
});

poke(self.listeners, 'error');
this.client.on('error', function (error) {
onClientError.call(self, autoReconnect, reconnectionDelay, error);
});

this.client.on('message', function (payload) {
Expand Down Expand Up @@ -237,4 +235,24 @@ function poke (listeners, roomId, payload) {
}
}

/**
* Called when the connection closes with an error state
*
* @param {boolean} autoReconnect
* @param {number} reconnectionDelay
* @param {string|Object} message
*/
function onClientError(autoReconnect, reconnectionDelay, message) {
var self = this;

if (autoReconnect) {
self.retrying = true;
setTimeout(function () {
self.connect(autoReconnect, reconnectionDelay);
}, reconnectionDelay);
}

poke(self.listeners, 'error', message);
}

module.exports = WSNode;
2 changes: 1 addition & 1 deletion test/kuzzle/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('Kuzzle constructor', () => {
kuzzle = new Kuzzle('nowhere', function (err, res) {
try {
should(err).be.instanceOf(Error);
should(err.message).be.exactly('Unable to connect to kuzzle server at "nowhere"');
should(err.message).be.exactly('Unable to connect to kuzzle proxy server at "nowhere"');
should(err.internal).be.exactly('error');
should(res).be.undefined();
should(kuzzle.state).be.exactly('error');
Expand Down
17 changes: 16 additions & 1 deletion test/network/wsbrowsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,27 @@ describe('WebSocket Browsers networking module', () => {
should(wsbrowsers.listeners.disconnect.length).be.eql(1);

wsbrowsers.connect();
clientStub.onclose();
clientStub.onclose(1000);

should(cb.calledOnce).be.true();
should(wsbrowsers.listeners.disconnect.length).be.eql(1);
});

it('should call error listeners on a disconnect event with an abnormal websocket code', () => {
var cb = sinon.stub();

wsbrowsers.retrying = false;
wsbrowsers.onConnectError(cb);
should(wsbrowsers.listeners.error.length).be.eql(1);

wsbrowsers.connect();
clientStub.onclose(4666, 'foobar');

should(cb.calledOnce).be.true();
should(cb.calledWith('foobar'));
should(wsbrowsers.listeners.error.length).be.eql(1);
});

it('should be able to register ephemeral callbacks on an event', () => {
var
cb = sinon.stub(),
Expand Down
17 changes: 16 additions & 1 deletion test/network/wsnode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,27 @@ describe('WebSocket NodeJS networking module', () => {
should(wsnode.listeners.disconnect.length).be.eql(1);

wsnode.connect();
clientStub.emit('close');
clientStub.emit('close', 1000);

should(cb.calledOnce).be.true();
should(wsnode.listeners.disconnect.length).be.eql(1);
});

it('should call error listeners on a disconnect event with an abnormal websocket code', () => {
var cb = sinon.stub();

wsnode.retrying = false;
wsnode.onConnectError(cb);
should(wsnode.listeners.error.length).be.eql(1);

wsnode.connect();
clientStub.emit('close', 4666, 'foobar');

should(cb.calledOnce).be.true();
should(cb.calledWith('foobar'));
should(wsnode.listeners.error.length).be.eql(1);
});

it('should be able to register ephemeral callbacks on an event', () => {
var
cb = sinon.stub(),
Expand Down