Merge pull request #1927 from emanuelschuetze/autoconnect

Refactor reconnect function of autoupdate.
This commit is contained in:
Oskar Hahn 2016-02-01 21:25:58 +01:00
commit 7d6c202200

View File

@ -39,40 +39,41 @@ angular.module('OpenSlidesApp.core', [
'DS', 'DS',
'$rootScope', '$rootScope',
function (DS, $rootScope) { function (DS, $rootScope) {
var url = location.origin + "/sockjs"; var socket = null;
var socket; var recInterval = null;
var Autoupdate = { $rootScope.connected = true;
socket: null,
message_receivers: [],
connect: function () {
var autoupdate = this;
socket = this.socket = new SockJS(url);
this.socket.onmessage = function (event) { var newConnect = function () {
_.forEach(autoupdate.message_receivers, function (receiver) { socket = new SockJS(location.origin + "/sockjs");
clearInterval(recInterval);
socket.onopen = function () {
$rootScope.connected = true;
};
socket.onclose = function () {
$rootScope.connected = false;
socket = null;
recInterval = setInterval(function () {
newConnect();
}, 1000);
};
};
var Autoupdate = {
messageReceivers: [],
connect: function () {
socket.onmessage = function (event) {
_.forEach(Autoupdate.messageReceivers, function (receiver) {
receiver(event.data); receiver(event.data);
}); });
}; };
this.socket.onopen = function () {
$rootScope.connected = true;
};
this.socket.onclose = function () {
$rootScope.connected = false;
autoupdate.connect()
};
}, },
on_message: function (receiver) { on_message: function (receiver) {
this.message_receivers.push(receiver); this.messageReceivers.push(receiver);
}, },
reconnect: function () { reconnect: function () {
socket.close(); socket.close();
Autoupdate.connect();
} }
}; };
$rootScope.connected = false; newConnect();
Autoupdate.connect(); Autoupdate.connect();
return Autoupdate; return Autoupdate;
} }