Refactoring WebSocket connection failure logic.

This completely decouples the connection failed event in kernel.js
from its handling in notificationarea.js.
Brian E. Granger 13 years ago
parent 0d94e67a47
commit b9193360a2

@ -110,36 +110,11 @@ var IPython = (function (IPython) {
};
Kernel.prototype._websocket_closed = function(ws_url, early){
var msg;
var parent_item = $('body');
if (early) {
msg = "Websocket connection to " + ws_url + " could not be established." +
" You will NOT be able to run code." +
" Your browser may not be compatible with the websocket version in the server," +
" or if the url does not look right, there could be an error in the" +
" server's configuration.";
} else {
IPython.notification_area.widget('kernel').set_message('Reconnecting Websockets', 1000);
this.start_channels();
return;
}
var dialog = $('<div/>');
dialog.html(msg);
parent_item.append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "Websocket closed",
closeText: "",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"OK": function () {
$(this).dialog('close');
}
}
});
Kernel.prototype._websocket_closed = function(ws_url, early) {
this.stop_channels();
$([IPython.events]).trigger('websocket_closed.Kernel',
{ws_url: ws_url, kernel: this, early: early}
);
};
/**
@ -152,7 +127,7 @@ var IPython = (function (IPython) {
var that = this;
this.stop_channels();
var ws_url = this.ws_url + this.kernel_url;
console.log("Starting WS:", ws_url);
console.log("Starting WebSockets:", ws_url);
this.shell_channel = new this.WebSocket(ws_url + "/shell");
this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
send_cookie = function(){
@ -182,9 +157,13 @@ var IPython = (function (IPython) {
this.iopub_channel.onopen = send_cookie;
this.iopub_channel.onclose = ws_closed_early;
// switch from early-close to late-close message after 1s
setTimeout(function(){
that.shell_channel.onclose = ws_closed_late;
that.iopub_channel.onclose = ws_closed_late;
setTimeout(function() {
if (that.shell_channel !== null) {
that.shell_channel.onclose = ws_closed_late;
}
if (that.iopub_channel !== null) {
that.iopub_channel.onclose = ws_closed_late;
}
}, 1000);
};

@ -93,25 +93,72 @@ var IPython = (function (IPython) {
$([IPython.events]).on('status_dead.Kernel',function () {
var dialog = $('<div/>');
dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
dialog.html('The kernel has died, would you like to restart it?' +
' If you do not restart the kernel, you will be able to save' +
' the notebook, but running code will not work until the notebook' +
' is reopened.'
);
$(document).append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "Dead kernel",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"Restart": function () {
$([IPython.events]).trigger('status_restarting.Kernel');
IPython.notebook.start_kernel();
$(this).dialog('close');
},
"Continue running": function () {
"Don't restart": function () {
$(this).dialog('close');
}
}
});
});
$([IPython.events]).on('websocket_closed.Kernel', function (event, data) {
var kernel = data.kernel;
var ws_url = data.ws_url;
var early = data.early;
var msg;
console.log(early);
if (!early) {
knw.set_message('Reconnecting WebSockets', 1000);
setTimeout(function () {
kernel.start_channels();
}, 5000);
return;
}
console.log('WebSocket connection failed: ', ws_url)
msg = "A WebSocket connection to could not be established." +
" You will NOT be able to run code. Check your" +
" network connection or notebook server configuration.";
var dialog = $('<div/>');
dialog.html(msg);
$(document).append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "WebSocket connection failed",
closeText: "",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"OK": function () {
$(this).dialog('close');
},
"Reconnect": function () {
knw.set_message('Reconnecting WebSockets', 1000);
setTimeout(function () {
kernel.start_channels();
}, 5000);
$(this).dialog('close');
}
}
});
});
var nnw = this.new_notification_widget('notebook');
// Notebook events

Loading…
Cancel
Save