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;
*/
// @ngInject
function connect($rootScope, annotationMapper, groups, session, settings) {
// Get the socket URL
var url = settings.websocketUrl;
// The public interface of the streamer. Returned from connect.
var controls = {
setConfig: setConfig
};
// Client configuration messages, to be sent each time a new connection is
// established.
var configMessages = {};
// Close any existing socket
if (socket) {
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
socket = new Socket(url);
......@@ -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 () {
sendClientConfig();
});
......@@ -110,21 +131,7 @@ 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.isConnected()) {
socket.send(configMessage);
}
}
return {
setConfig: setConfig,
};
return controls;
}
module.exports = {
......
......@@ -6,7 +6,7 @@ var EventEmitter = require('tiny-emitter');
var proxyquire = require('proxyquire');
// the most recently created FakeSocket instance
var fakeWebSocket;
var fakeWebSocket = null;
function FakeSocket(url) {
fakeWebSocket = this;
......@@ -37,6 +37,16 @@ describe('streamer', function () {
var fakeSettings;
var activeStreamer;
function createDefaultStreamer() {
activeStreamer = streamer.connect(
fakeRootScope,
fakeAnnotationMapper,
fakeGroups,
fakeSession,
fakeSettings
);
}
beforeEach(function () {
fakeRootScope = {
$apply: function (callback) {
......@@ -66,23 +76,23 @@ describe('streamer', function () {
streamer = proxyquire('../streamer', {
'./websocket': FakeSocket,
});
});
activeStreamer = streamer.connect(
fakeRootScope,
fakeAnnotationMapper,
fakeGroups,
fakeSession,
fakeSettings
);
it('should not create a websocket connection if websocketUrl is not provided', function () {
fakeSettings = {}
createDefaultStreamer();
assert.isNull(fakeWebSocket);
});
it('should send a client ID', function () {
createDefaultStreamer();
assert.equal(fakeWebSocket.messages.length, 1);
assert.equal(fakeWebSocket.messages[0].messageType, 'client_id');
assert.equal(fakeWebSocket.messages[0].value, streamer.clientId);
});
it('should close any existing socket', function () {
createDefaultStreamer();
var oldStreamer = activeStreamer;
var oldWebSocket = fakeWebSocket;
var newStreamer = streamer.connect(
......@@ -98,6 +108,7 @@ describe('streamer', function () {
describe('annotation notifications', function () {
it('should load new annotations', function () {
createDefaultStreamer();
fakeWebSocket.notify({
type: 'annotation-notification',
options: {
......@@ -111,6 +122,7 @@ describe('streamer', function () {
});
it('should unload deleted annotations', function () {
createDefaultStreamer();
fakeWebSocket.notify({
type: 'annotation-notification',
options: {
......@@ -126,6 +138,7 @@ describe('streamer', function () {
describe('session change notifications', function () {
it('updates the session when a notification is received', function () {
createDefaultStreamer();
var model = {
groups: [{
id: 'new-group'
......@@ -141,6 +154,7 @@ describe('streamer', function () {
describe('reconnections', function () {
it('resends configuration messages when a reconnection occurs', function () {
createDefaultStreamer();
fakeWebSocket.messages = [];
fakeWebSocket.emit('open');
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