-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathdjng-websocket.js
185 lines (165 loc) · 4.8 KB
/
djng-websocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(function(angular, undefined) {
'use strict';
function noop() {}
// Add three-way data-binding for AngularJS with Django using websockets.
var djng_ws_module = angular.module('djng.websocket', []);
// Wraps the built-in WebSocket into a replaceable provider suitable for dependency injection.
djng_ws_module.service('$websocket', function() {
var ws;
this.connect = function(url) {
ws = new WebSocket(url);
ws.onopen = this.onopen;
ws.onmessage = this.onmessage;
ws.onerror = this.onerror;
ws.onclose = this.onclose;
};
this.send = function(msg) {
ws.send(msg);
};
this.close = function() {
ws.close();
};
});
djng_ws_module.provider('djangoWebsocket', function() {
var _console = { log: noop, warn: noop, error: noop };
var websocket_uri, heartbeat_msg = null;
var $log = angular.injector(['ng']).get('$log');
// Set prefix for the Websocket's URI.
// This URI must be set during initialization using
// djangoWebsocketProvider.setURI('{{ WEBSOCKET_URI }}');
this.setURI = function(uri) {
websocket_uri = uri;
return this;
};
// Set the heartbeat message and activate the heartbeat interval to 5 seconds.
// The heartbeat message shall be configured using
// djangoWebsocketProvider.setHeartbeat({{ WS4REDIS_HEARTBEAT }}); // unquoted!
// The default behavior is to not listen on heartbeats.
this.setHeartbeat = function(msg) {
heartbeat_msg = msg;
return this;
};
this.setLogLevel = function(logLevel) {
switch (logLevel) {
case 'debug':
_console = $log;
break;
case 'log':
_console.log = $log.log;
/* falls through */
case 'warn':
_console.warn = $log.warn;
/* falls through */
case 'error':
_console.error = $log.error;
/* falls through */
default:
break;
}
return this;
};
this.$get = ['$websocket', '$q', '$timeout', '$interval', function($websocket, $q, $timeout, $interval) {
var ws_url, deferred, scope, collection;
var is_subscriber = false, is_publisher = false, receiving = false;
var wait_for_reconnect = 0, heartbeat_promise = null, missed_heartbeats = 0;
function connect() {
_console.log("Connecting to "+ws_url);
deferred = $q.defer();
$websocket.connect(ws_url);
}
$websocket.onopen = function(evt) {
_console.log('Connected');
deferred.resolve();
wait_for_reconnect = 0;
if (heartbeat_msg && heartbeat_promise === null) {
missed_heartbeats = 0;
heartbeat_promise = $interval(sendHeartbeat, 5000);
}
};
$websocket.onclose = function(evt) {
_console.log("Disconnected");
deferred.reject();
wait_for_reconnect = Math.min(wait_for_reconnect + 1000, 10000);
$timeout(function() {
$websocket.connect(ws_url);
}, wait_for_reconnect);
};
$websocket.onerror = function(evt) {
_console.error("Websocket connection is broken!");
$websocket.close();
};
$websocket.onmessage = function(evt) {
var data;
if (evt.data === heartbeat_msg) {
// reset the counter for missed heartbeats
missed_heartbeats = 0;
return;
}
try {
data = angular.fromJson(evt.data);
} catch(e) {
_console.warn('Data received by server is invalid JSON: ' + evt.data);
return;
}
if (is_subscriber) {
// temporarily disable the function 'listener', so that message received
// from the websocket, are not propagated back
receiving = true;
scope.$apply(function() {
angular.extend(scope[collection], data);
});
receiving = false;
}
};
function sendHeartbeat() {
try {
missed_heartbeats++;
if (missed_heartbeats > 3)
throw new Error("Too many missed heartbeats.");
$websocket.send(heartbeat_msg);
} catch(e) {
$interval.cancel(heartbeat_promise);
heartbeat_promise = null;
_console.warn("Closing connection. Reason: " + e.message);
$websocket.close();
}
}
function listener(newValue, oldValue) {
if (!receiving && !angular.equals(oldValue, newValue)) {
$websocket.send(angular.toJson(newValue));
}
}
function setChannels(channels) {
angular.forEach(channels, function(channel) {
if (channel.substring(0, 9) === 'subscribe') {
is_subscriber = true;
} else if (channel.substring(0, 7) === 'publish') {
is_publisher = true;
}
});
}
function watchCollection() {
scope.$watchCollection(collection, listener);
}
function buildWebsocketURL(facility, channels) {
var parts = [websocket_uri, facility, '?'];
parts.push(channels.join('&'));
ws_url = parts.join('');
}
return {
connect: function($scope, scope_obj, facility, channels) {
scope = $scope;
setChannels(channels);
collection = scope_obj;
scope[collection] = scope[collection] || {};
buildWebsocketURL(facility, channels);
connect();
if (is_publisher) {
deferred.promise.then(watchCollection);
}
return deferred.promise;
}
};
}];
});
})(window.angular);