Commit 3b2bd157 authored by Nick Stenning's avatar Nick Stenning

Merge pull request #2746 from robertknight/websocket-send-on-reconnect

Fix sending of WebSocket messages enqueued during a disconnection
parents 9a36b66d 96c48706
...@@ -33,6 +33,20 @@ describe('websocket wrapper', function () { ...@@ -33,6 +33,20 @@ describe('websocket wrapper', function () {
assert.notEqual(fakeSocket, initialSocket); assert.notEqual(fakeSocket, initialSocket);
}); });
it('should send queued messages after a reconnect', function () {
// simulate WebSocket setup and initial connection
var socket = new Socket('ws://test:1234');
fakeSocket.onopen({});
// simulate abnormal disconnection
fakeSocket.onclose({code: 1006});
// enqueue a message and check that it is sent after the WS reconnects
socket.send({aKey: 'aValue'});
fakeSocket.onopen({});
assert.calledWith(fakeSocket.send, '{"aKey":"aValue"}');
});
it('should not reconnect after a normal disconnection', function () { it('should not reconnect after a normal disconnection', function () {
var socket = new Socket('ws://test:1234'); var socket = new Socket('ws://test:1234');
socket.close(); socket.close();
......
...@@ -22,7 +22,7 @@ function Socket(url) { ...@@ -22,7 +22,7 @@ function Socket(url) {
// queue of JSON objects which have not yet been submitted // queue of JSON objects which have not yet been submitted
var messageQueue = []; var messageQueue = [];
// the current WebSocket instance or null if disconnected // the current WebSocket instance
var socket; var socket;
function sendMessages() { function sendMessages() {
...@@ -58,7 +58,6 @@ function Socket(url) { ...@@ -58,7 +58,6 @@ function Socket(url) {
connectOperation.retry(new Error(event.reason)); connectOperation.retry(new Error(event.reason));
} }
} }
socket = null;
self.emit('close', event); self.emit('close', event);
}; };
...@@ -74,10 +73,6 @@ function Socket(url) { ...@@ -74,10 +73,6 @@ function Socket(url) {
/** Close the underlying WebSocket connection */ /** Close the underlying WebSocket connection */
this.close = function () { this.close = function () {
if (!socket) {
console.error('Socket.close() called before socket was connected');
return;
}
socket.close(); socket.close();
}; };
...@@ -94,7 +89,7 @@ function Socket(url) { ...@@ -94,7 +89,7 @@ function Socket(url) {
/** Returns true if the WebSocket is currently connected. */ /** Returns true if the WebSocket is currently connected. */
this.isConnected = function () { this.isConnected = function () {
return socket && socket.readyState === WebSocket.OPEN; return socket.readyState === WebSocket.OPEN;
}; };
// establish the initial connection // establish the initial connection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment