From 7cb0882c7339427ca95df6b70ea5a60f1fcfefd6 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 28 Mar 2016 21:29:23 -0500 Subject: [PATCH] Add "force" option to allowReconnect --- R/shiny.R | 21 ++++++++++++--------- man/session.Rd | 17 ++++++++++------- srcjs/shinyapp.js | 19 +++++++++---------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/R/shiny.R b/R/shiny.R index cc7f92d8a..e722ef9cc 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -242,13 +242,16 @@ workerId <- local({ #' (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). +#' If \code{value} is \code{TRUE} and run in a hosting environment (Shiny +#' Server or Connect) with reconnections enabled, then when the session ends +#' due to the network connection closing, the client will 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). If \code{"force"}, then the client browser will always +#' attempt to reconnect. The only reason to use \code{"force"} is for testing +#' on a local connection (without Shiny Server or Connect). #' } #' \item{sendCustomMessage(type, message)}{ #' Sends a custom message to the web page. \code{type} must be a @@ -541,8 +544,8 @@ ShinySession <- R6Class( }, allowReconnect = function(value) { - if (!(identical(value, TRUE) || identical(value, FALSE))) { - stop("value must be TRUE or FALSE") + if (!(identical(value, TRUE) || identical(value, FALSE) || identical(value, "force"))) { + stop('value must be TRUE, FALSE, or "force"') } private$write(toJSON(list(allowReconnect = value))) }, diff --git a/man/session.Rd b/man/session.Rd index 145d8d28e..59bd79f06 100644 --- a/man/session.Rd +++ b/man/session.Rd @@ -86,13 +86,16 @@ (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). + If \code{value} is \code{TRUE} and run in a hosting environment (Shiny + Server or Connect) with reconnections enabled, then when the session ends + due to the network connection closing, the client will 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). If \code{"force"}, then the client browser will always + attempt to reconnect. The only reason to use \code{"force"} is for testing + on a local connection (without Shiny Server or Connect). } \item{sendCustomMessage(type, message)}{ Sends a custom message to the web page. \code{type} must be a diff --git a/srcjs/shinyapp.js b/srcjs/shinyapp.js index 70c58f630..4e6145007 100644 --- a/srcjs/shinyapp.js +++ b/srcjs/shinyapp.js @@ -82,12 +82,6 @@ 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; }; @@ -225,8 +219,12 @@ var ShinyApp = function() { } // To try a reconnect, both the app (this.$allowReconnect) and the - // server (this.$socket.allowReconnect) must allow reconnections. - if (this.$allowReconnect === true && this.$socket.allowReconnect === true) { + // server (this.$socket.allowReconnect) must allow reconnections, or + // session$allowReconnect("force") was called. The "force" option should + // only be used for testing. + if ((this.$allowReconnect === true && this.$socket.allowReconnect === true) || + this.$allowReconnect === "force") + { var delay = reconnectDelay.next(); exports.showReconnectDialog(delay); this.$scheduleReconnect(delay); @@ -591,10 +589,11 @@ var ShinyApp = function() { }); addMessageHandler('allowReconnect', function(message) { - if (!(message === true || message === false)) { + if (message === true || message === false || message === "force") { + this.$allowReconnect = message; + } else { throw "Invalid value for allowReconnect: " + message; } - this.$allowReconnect = message; }); addMessageHandler('custom', function(message) {