Browse Source

Improved reconnect logic which failed in various networking szenarios.

pull/182/head
Simon Eisenmann 11 years ago
parent
commit
ecb3649fe7
  1. 28
      static/js/controllers/mediastreamcontroller.js
  2. 47
      static/js/mediastream/connector.js

28
static/js/controllers/mediastreamcontroller.js

@ -44,6 +44,7 @@ define(['jquery', 'underscore', 'angular', 'bigscreen', 'moment', 'sjcl', 'moder
mediaStream.webrtc.doHangup("unload"); mediaStream.webrtc.doHangup("unload");
if (mediaStream.api.connector) { if (mediaStream.api.connector) {
mediaStream.api.connector.disabled = true; mediaStream.api.connector.disabled = true;
mediaStream.api.connector.close();
} }
}); });
@ -523,15 +524,19 @@ define(['jquery', 'underscore', 'angular', 'bigscreen', 'moment', 'sjcl', 'moder
} }
console.log("Stored data at the resurrection shrine", resurrect); console.log("Stored data at the resurrection shrine", resurrect);
} }
reconnecting = false; if (!reconnecting) {
_.delay(function() { reconnecting = true;
if (autoreconnect && !reconnecting) { _.delay(function() {
reconnecting = true; if (autoreconnect) {
console.log("Requesting to reconnect ..."); console.log("Requesting to reconnect ...");
mediaStream.reconnect(); mediaStream.reconnect();
} }
}, 500); reconnecting = false;
$scope.setStatus("reconnecting"); }, 500);
$scope.setStatus("reconnecting");
} else {
console.warn("Already reconnecting ...");
}
} else { } else {
$scope.setStatus("closed"); $scope.setStatus("closed");
} }
@ -541,7 +546,6 @@ define(['jquery', 'underscore', 'angular', 'bigscreen', 'moment', 'sjcl', 'moder
// TODO(lcooper): Is it really needful to do this stuff? // TODO(lcooper): Is it really needful to do this stuff?
$timeout.cancel(ttlTimeout); $timeout.cancel(ttlTimeout);
connected = true; connected = true;
reconnecting = false;
$scope.updateStatus(true); $scope.updateStatus(true);
}); });
@ -551,13 +555,11 @@ define(['jquery', 'underscore', 'angular', 'bigscreen', 'moment', 'sjcl', 'moder
switch (event.type) { switch (event.type) {
case "open": case "open":
connected = true; connected = true;
reconnecting = false;
$scope.updateStatus(true); $scope.updateStatus(true);
$scope.setStatus("waiting"); $scope.setStatus("waiting");
break; break;
case "error": case "error":
if (reconnecting || connected) { if (connected) {
reconnecting = false;
reconnect(); reconnect();
} else { } else {
$scope.setStatus(event.type); $scope.setStatus(event.type);

47
static/js/mediastream/connector.js

@ -44,6 +44,11 @@ define(['jquery', 'underscore'], function($, _) {
return; return;
} }
if (this.connecting !== null) {
console.warn("Refusing to connect while already connecting");
return;
}
this.error = false; this.error = false;
this.e.triggerHandler("connecting", [url]); this.e.triggerHandler("connecting", [url]);
this.url = url; this.url = url;
@ -51,11 +56,31 @@ define(['jquery', 'underscore'], function($, _) {
url += ("?t=" + this.token); url += ("?t=" + this.token);
//console.log("Reusing existing token", this.token); //console.log("Reusing existing token", this.token);
} }
var that = this;
// Create connection.
var conn = this.conn = new WebSocket(url); var conn = this.conn = new WebSocket(url);
conn.onopen = _.bind(this.onopen, this); conn.onopen = function(event) {
conn.onerror = _.bind(this.onerror, this); if (event.target === that.conn) {
conn.onclose = _.bind(this.onclose, this); that.onopen(event);
conn.onmessage = _.bind(this.onmessage, this) }
};
conn.onerror = function(event) {
if (event.target === that.conn) {
that.onerror(event);
}
};
conn.onclose = function(event) {
if (event.target === that.conn) {
that.onclose(event);
}
};
conn.onmessage = function(event) {
if (event.target === that.conn) {
that.onmessage(event);
}
};
this.connecting = window.setTimeout(_.bind(function() { this.connecting = window.setTimeout(_.bind(function() {
console.warn("Connection timeout out after", this.connecting_timeout); console.warn("Connection timeout out after", this.connecting_timeout);
@ -97,14 +122,14 @@ define(['jquery', 'underscore'], function($, _) {
Connector.prototype.close = function() { Connector.prototype.close = function() {
window.clearTimeout(this.connecting);
this.connecting = null;
this.connected = false; this.connected = false;
if (this.conn) { if (this.conn) {
var conn = this.conn; var conn = this.conn;
this.conn = null; this.conn = null;
if (!this.error) { conn.close();
conn.close();
}
conn.onopen = conn.onerror = conn.onclose = conn.onmessage = null;
} }
}; };
@ -119,7 +144,9 @@ define(['jquery', 'underscore'], function($, _) {
}; };
Connector.prototype.onopen = function(event) { Connector.prototype.onopen = function(event) {
window.clearTimeout(this.connecting); window.clearTimeout(this.connecting);
this.connecting = null;
this.connecting_timeout = timeout; this.connecting_timeout = timeout;
// Connection successfully established. // Connection successfully established.
@ -133,11 +160,13 @@ define(['jquery', 'underscore'], function($, _) {
data = this.queue.shift(); data = this.queue.shift();
this.send(data); this.send(data);
} }
}; };
Connector.prototype.onerror = function(event) { Connector.prototype.onerror = function(event) {
window.clearTimeout(this.connecting); window.clearTimeout(this.connecting);
this.connecting = null;
this.connecting_timeout = timeout; this.connecting_timeout = timeout;
//console.log("onerror", event); //console.log("onerror", event);
@ -151,6 +180,7 @@ define(['jquery', 'underscore'], function($, _) {
Connector.prototype.onclose = function(event) { Connector.prototype.onclose = function(event) {
window.clearTimeout(this.connecting); window.clearTimeout(this.connecting);
this.connecting = null;
this.connecting_timeout = timeout; this.connecting_timeout = timeout;
//console.log("onclose", event); //console.log("onclose", event);
@ -159,6 +189,7 @@ define(['jquery', 'underscore'], function($, _) {
if (!this.error) { if (!this.error) {
this.e.triggerHandler("close", [null, event]); this.e.triggerHandler("close", [null, event]);
} }
}; };
Connector.prototype.onmessage = function(event) { Connector.prototype.onmessage = function(event) {

Loading…
Cancel
Save