From 2bce1da34fc8c34e509fb98b38e2c02b0a97905b Mon Sep 17 00:00:00 2001 From: Alan Dipert Date: Thu, 23 Aug 2018 16:03:28 -0700 Subject: [PATCH] Add reactiveWebSocket for ease of use with Shiny --- R/websocket.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/R/websocket.R b/R/websocket.R index 7de9343..70ca51a 100644 --- a/R/websocket.R +++ b/R/websocket.R @@ -185,3 +185,21 @@ WebsocketClient <- R6::R6Class("WebsocketClient", } ) ) + +#' @export +reactiveWebSocket <- function(url) { + rv <- reactiveVal() + err <- reactiveVal(FALSE) + ws <- WebsocketClient$new(url, onMessage = function(msg) { + rv(msg) + }, onFail = function() { + err(TRUE) + }) + reactive({ + if (err()) { + stop("WebSocket error") + } else { + rv() + } + }) +}