Allow app to control reconnection behavior

This commit is contained in:
Winston Chang
2016-01-08 16:48:30 -06:00
parent 3452a445fe
commit dca527d8b6
3 changed files with 48 additions and 1 deletions

View File

@@ -241,6 +241,15 @@ workerId <- local({
#' This is the request that was used to initiate the websocket connection
#' (as opposed to the request that downloaded the web page for the app).
#' }
#' \item{allowReconnect(value)}{
#' If \code{value} is \code{TRUE}, this tells the browser that, if the
#' session ends due to the network connection closing, attempt to reconnect
#' to the server. If a reconnection is successful, the browser will send all
#' the current input values to the new session on the server, and the server
#' will recalculate any outputs and send them back to the client. If
#' \code{value} is \code{FALSE}, reconnections will be disabled (this is the
#' default state).
#' }
#' \item{sendCustomMessage(type, message)}{
#' Sends a custom message to the web page. \code{type} must be a
#' single-element character vector giving the type of message, while
@@ -530,6 +539,14 @@ ShinySession <- R6Class(
setShowcase = function(value) {
private$showcase <- !is.null(value) && as.logical(value)
},
allowReconnect = function(value) {
if (!(identical(value, TRUE) || identical(value, FALSE))) {
stop("value must be TRUE or FALSE")
}
private$write(toJSON(list(allowReconnect = value)))
},
defineOutput = function(name, func, label) {
"Binds an output generating function to this name. The function can either
take no parameters, or have named parameters for \\code{name} and

View File

@@ -85,6 +85,15 @@
This is the request that was used to initiate the websocket connection
(as opposed to the request that downloaded the web page for the app).
}
\item{allowReconnect(value)}{
If \code{value} is \code{TRUE}, this tells the browser that, if the
session ends due to the network connection closing, attempt to reconnect
to the server. If a reconnection is successful, the browser will send all
the current input values to the new session on the server, and the server
will recalculate any outputs and send them back to the client. If
\code{value} is \code{FALSE}, reconnections will be disabled (this is the
default state).
}
\item{sendCustomMessage(type, message)}{
Sends a custom message to the web page. \code{type} must be a
single-element character vector giving the type of message, while

View File

@@ -17,6 +17,8 @@ var ShinyApp = function() {
this.$pendingMessages = [];
this.$activeRequests = {};
this.$nextRequestId = 0;
this.$allowReconnect = false;
};
(function() {
@@ -74,6 +76,13 @@ var ShinyApp = function() {
var ws = new WebSocket(protocol + '//' + window.location.host + defaultPath);
ws.binaryType = 'arraybuffer';
// This flag indicates that reconnections are permitted by the server.
// When Shiny is running with Shiny Server, this flag will be present
// only on versions of SS that support reconnects. When running Shiny
// locally, this is set to true, because the "server" always permits
// reconnection.
ws.allowReconnect = true;
return ws;
};
@@ -118,7 +127,12 @@ var ShinyApp = function() {
}
self.$removeSocket();
self.$scheduleReconnect();
// To try a reconnect, both the app (self.$allowReconnect) and the
// server (socket.allowReconnect) must allow reconnections.
if (self.$allowReconnect === true && socket.allowReconnect === true) {
self.$scheduleReconnect();
}
};
return socket;
};
@@ -563,6 +577,13 @@ var ShinyApp = function() {
}
});
addMessageHandler('allowReconnect', function(message) {
if (!(message === true || message === false)) {
throw "Invalid value for allowReconnect: " + message;
}
this.$allowReconnect = message;
});
addMessageHandler('custom', function(message) {
// For old-style custom messages - should deprecate and migrate to new
// method