Commit ec3d389f authored by Nick Stenning's avatar Nick Stenning

Make connecting the WebSocket optional

In preparation for splitting the hosting of the WebSocket server into a
separate process, make it possible to run the client without attempting
to connect to the WebSocket.

If the `websocketUrl` setting is missing, we will not attempt to
connect.
parent 4b9d4be3
...@@ -26,15 +26,24 @@ var socket; ...@@ -26,15 +26,24 @@ var socket;
*/ */
// @ngInject // @ngInject
function connect($rootScope, annotationMapper, groups, session, settings) { function connect($rootScope, annotationMapper, groups, session, settings) {
// Get the socket URL // The public interface of the streamer. Returned from connect.
var url = settings.websocketUrl; var controls = {
setConfig: setConfig
};
// Client configuration messages, to be sent each time a new connection is
// established.
var configMessages = {};
// Close any existing socket // Close any existing socket
if (socket) { if (socket) {
socket.close(); socket.close();
} }
var configMessages = {}; // If we have no URL configured, don't do anything.
var url = settings.websocketUrl;
if (!url) {
return controls;
}
// Open the socket // Open the socket
socket = new Socket(url); socket = new Socket(url);
...@@ -82,6 +91,18 @@ function connect($rootScope, annotationMapper, groups, session, settings) { ...@@ -82,6 +91,18 @@ function connect($rootScope, annotationMapper, groups, session, settings) {
}); });
} }
/**
* Send a configuration message to the push notification service.
* Each message is associated with a key, which is used to re-send
* configuration data to the server in the event of a reconnection.
*/
function setConfig(key, configMessage) {
configMessages[key] = configMessage;
if (socket && socket.isConnected()) {
socket.send(configMessage);
}
}
socket.on('open', function () { socket.on('open', function () {
sendClientConfig(); sendClientConfig();
}); });
...@@ -110,21 +131,7 @@ function connect($rootScope, annotationMapper, groups, session, settings) { ...@@ -110,21 +131,7 @@ function connect($rootScope, annotationMapper, groups, session, settings) {
}); });
}); });
/** return controls;
* Send a configuration message to the push notification service.
* Each message is associated with a key, which is used to re-send
* configuration data to the server in the event of a reconnection.
*/
function setConfig(key, configMessage) {
configMessages[key] = configMessage;
if (socket.isConnected()) {
socket.send(configMessage);
}
}
return {
setConfig: setConfig,
};
} }
module.exports = { module.exports = {
......
...@@ -6,7 +6,7 @@ var EventEmitter = require('tiny-emitter'); ...@@ -6,7 +6,7 @@ var EventEmitter = require('tiny-emitter');
var proxyquire = require('proxyquire'); var proxyquire = require('proxyquire');
// the most recently created FakeSocket instance // the most recently created FakeSocket instance
var fakeWebSocket; var fakeWebSocket = null;
function FakeSocket(url) { function FakeSocket(url) {
fakeWebSocket = this; fakeWebSocket = this;
...@@ -37,6 +37,16 @@ describe('streamer', function () { ...@@ -37,6 +37,16 @@ describe('streamer', function () {
var fakeSettings; var fakeSettings;
var activeStreamer; var activeStreamer;
function createDefaultStreamer() {
activeStreamer = streamer.connect(
fakeRootScope,
fakeAnnotationMapper,
fakeGroups,
fakeSession,
fakeSettings
);
}
beforeEach(function () { beforeEach(function () {
fakeRootScope = { fakeRootScope = {
$apply: function (callback) { $apply: function (callback) {
...@@ -66,23 +76,23 @@ describe('streamer', function () { ...@@ -66,23 +76,23 @@ describe('streamer', function () {
streamer = proxyquire('../streamer', { streamer = proxyquire('../streamer', {
'./websocket': FakeSocket, './websocket': FakeSocket,
}); });
});
activeStreamer = streamer.connect( it('should not create a websocket connection if websocketUrl is not provided', function () {
fakeRootScope, fakeSettings = {}
fakeAnnotationMapper, createDefaultStreamer();
fakeGroups, assert.isNull(fakeWebSocket);
fakeSession,
fakeSettings
);
}); });
it('should send a client ID', function () { it('should send a client ID', function () {
createDefaultStreamer();
assert.equal(fakeWebSocket.messages.length, 1); assert.equal(fakeWebSocket.messages.length, 1);
assert.equal(fakeWebSocket.messages[0].messageType, 'client_id'); assert.equal(fakeWebSocket.messages[0].messageType, 'client_id');
assert.equal(fakeWebSocket.messages[0].value, streamer.clientId); assert.equal(fakeWebSocket.messages[0].value, streamer.clientId);
}); });
it('should close any existing socket', function () { it('should close any existing socket', function () {
createDefaultStreamer();
var oldStreamer = activeStreamer; var oldStreamer = activeStreamer;
var oldWebSocket = fakeWebSocket; var oldWebSocket = fakeWebSocket;
var newStreamer = streamer.connect( var newStreamer = streamer.connect(
...@@ -98,6 +108,7 @@ describe('streamer', function () { ...@@ -98,6 +108,7 @@ describe('streamer', function () {
describe('annotation notifications', function () { describe('annotation notifications', function () {
it('should load new annotations', function () { it('should load new annotations', function () {
createDefaultStreamer();
fakeWebSocket.notify({ fakeWebSocket.notify({
type: 'annotation-notification', type: 'annotation-notification',
options: { options: {
...@@ -111,6 +122,7 @@ describe('streamer', function () { ...@@ -111,6 +122,7 @@ describe('streamer', function () {
}); });
it('should unload deleted annotations', function () { it('should unload deleted annotations', function () {
createDefaultStreamer();
fakeWebSocket.notify({ fakeWebSocket.notify({
type: 'annotation-notification', type: 'annotation-notification',
options: { options: {
...@@ -126,6 +138,7 @@ describe('streamer', function () { ...@@ -126,6 +138,7 @@ describe('streamer', function () {
describe('session change notifications', function () { describe('session change notifications', function () {
it('updates the session when a notification is received', function () { it('updates the session when a notification is received', function () {
createDefaultStreamer();
var model = { var model = {
groups: [{ groups: [{
id: 'new-group' id: 'new-group'
...@@ -141,6 +154,7 @@ describe('streamer', function () { ...@@ -141,6 +154,7 @@ describe('streamer', function () {
describe('reconnections', function () { describe('reconnections', function () {
it('resends configuration messages when a reconnection occurs', function () { it('resends configuration messages when a reconnection occurs', function () {
createDefaultStreamer();
fakeWebSocket.messages = []; fakeWebSocket.messages = [];
fakeWebSocket.emit('open'); fakeWebSocket.emit('open');
assert.equal(fakeWebSocket.messages.length, 1); assert.equal(fakeWebSocket.messages.length, 1);
......
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