Another fix for WebRTC

Cleanup the code that deals with detecting connection failures. This
change results in a more stable experience on Firefox. It also ensures
that any closed WebRTC connection is cleaned up properly. This means
that if the Companion disconnects due to the Companion being closed, or
a network failure, a new connection can be made reliably without having
to reload App Inventor in the browser.

Change-Id: I35ccba26729d7c870201165cb3821991a6f523ee
parent 3d058a32
...@@ -1613,6 +1613,7 @@ Blockly.Msg.en.switch_language_to_english = { ...@@ -1613,6 +1613,7 @@ Blockly.Msg.en.switch_language_to_english = {
Blockly.Msg.REPL_DO_YOU_REALLY_Q = "Do You Really?"; Blockly.Msg.REPL_DO_YOU_REALLY_Q = "Do You Really?";
Blockly.Msg.REPL_FACTORY_RESET = 'This will attempt to reset your Emulator to its "factory" state. If you had previously updated the Companion installed in the Emulator, you will likely have to do this again.'; Blockly.Msg.REPL_FACTORY_RESET = 'This will attempt to reset your Emulator to its "factory" state. If you had previously updated the Companion installed in the Emulator, you will likely have to do this again.';
Blockly.Msg.REPL_WEBRTC_CONNECTION_ERROR = "Companion Connection Error"; Blockly.Msg.REPL_WEBRTC_CONNECTION_ERROR = "Companion Connection Error";
Blockly.Msg.REPL_WEBRTC_CONNECTION_CLOSED = "The Companion has disconnected";
Blockly.Msg.REPL_EMULATOR_ONLY = 'You can only use this option to update the Emulator'; Blockly.Msg.REPL_EMULATOR_ONLY = 'You can only use this option to update the Emulator';
// Messages from Blockly.js // Messages from Blockly.js
......
...@@ -380,15 +380,7 @@ Blockly.ReplMgr.putYail = (function() { ...@@ -380,15 +380,7 @@ Blockly.ReplMgr.putYail = (function() {
var haveoffer = false; var haveoffer = false;
var connectionstate = "none"; var connectionstate = "none";
var webrtcerror = function(doalert, msg) { var webrtcerror = function(doalert, msg) {
webrtcdata = null; engine.resetcompanion();
webrtcstarting = false;
webrtcrunning = false;
webrtcforcestop = true;
top.BlocklyPanel_indicateDisconnect();
top.ConnectProgressBar_hide();
if (!webrtcpeer) {
webrtcpeer.close();
}
if (doalert) { if (doalert) {
var dialog = new Blockly.Util.Dialog(Blockly.Msg.REPL_NETWORK_ERROR, msg, Blockly.Msg.REPL_OK, false, null, 0, var dialog = new Blockly.Util.Dialog(Blockly.Msg.REPL_NETWORK_ERROR, msg, Blockly.Msg.REPL_OK, false, null, 0,
function() { function() {
...@@ -444,11 +436,24 @@ Blockly.ReplMgr.putYail = (function() { ...@@ -444,11 +436,24 @@ Blockly.ReplMgr.putYail = (function() {
}; };
webrtcpeer = new RTCPeerConnection(top.ReplState.iceservers); webrtcpeer = new RTCPeerConnection(top.ReplState.iceservers);
webrtcpeer.oniceconnectionstatechange = function(evt) { webrtcpeer.oniceconnectionstatechange = function(evt) {
console.log("oniceconnectionstatechange: evt.type = " + evt.type + " connection state = " + this.iceConnectionState); //////////////////////////////////////////////////////////////
connectionstate = webrtcpeer.iceConnectionState; // So Firefox will transiently issue an iceConnectionState //
if (connectionstate == "disconnected" || // of "disconnected" when everything is fine. It usually //
connectionstate == "failed") { // immediately issues a new event declaring the //
webrtcerror(true, Blockly.Msg.REPL_WEBRTC_CONNECTION_ERROR + "\n" + "state = " + connectionstate); // iceConnectionState as "connected". When the connection //
// is really dead, Firefox issues an event with //
// iceConnectionState of failed. Chrome on the other-hand //
// never issues an event with iceConnectionState of failed, //
// but just disconnected. The detection method below was //
// found on Stack Overflow. //
//////////////////////////////////////////////////////////////
var isFirefox = typeof InstallTrigger !== 'undefined';
console.log("oniceconnectionstatechange: evt.type = " + evt.type + " ice connection state = " +
this.iceConnectionState);
connectionstate = this.iceConnectionState;
if ((connectionstate == "disconnected" && !isFirefox) ||
connectionstate == "failed") {
webrtcerror(true, Blockly.Msg.REPL_WEBRTC_CONNECTION_CLOSED);
} }
}; };
webrtcpeer.onsignalingstatechange = function(evt) { webrtcpeer.onsignalingstatechange = function(evt) {
...@@ -795,6 +800,8 @@ Blockly.ReplMgr.putYail = (function() { ...@@ -795,6 +800,8 @@ Blockly.ReplMgr.putYail = (function() {
if (top.usewebrtc) { if (top.usewebrtc) {
if (webrtcdata) { if (webrtcdata) {
webrtcdata.close(); webrtcdata.close();
}
if (webrtcpeer) {
webrtcpeer.close(); webrtcpeer.close();
} }
webrtcforcestop = true; webrtcforcestop = true;
...@@ -802,12 +809,10 @@ Blockly.ReplMgr.putYail = (function() { ...@@ -802,12 +809,10 @@ Blockly.ReplMgr.putYail = (function() {
webrtcrunning = false; webrtcrunning = false;
webrtcstarting = false; webrtcstarting = false;
} }
if (rxhr) if (rxhr) {
rxhr.abort(); rxhr.abort();
}
rxhr = null; rxhr = null;
// if (conn) // This seems to cause disconnects on project switch
// conn.abort();
// conn = null;
top.usewebrtc = false; top.usewebrtc = false;
phonereceiving = false; phonereceiving = false;
}, },
...@@ -821,6 +826,7 @@ Blockly.ReplMgr.putYail = (function() { ...@@ -821,6 +826,7 @@ Blockly.ReplMgr.putYail = (function() {
// context.hardreset(context.formName); // kill adb and emulator // context.hardreset(context.formName); // kill adb and emulator
rs.didversioncheck = false; rs.didversioncheck = false;
top.BlocklyPanel_indicateDisconnect(); top.BlocklyPanel_indicateDisconnect();
top.ConnectProgressBar_hide();
engine.reset(); engine.reset();
}, },
"checkversionupgrade" : function(fatal, installer, force) { "checkversionupgrade" : function(fatal, installer, force) {
......
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