onWSOpen = function(ws) {
# The ws object is a WebSocket object
cat("New connection opened.\n")
# Capture client messages
ws$onMessage(function(binary, message) {
# Save the Response
input_message <- jsonlite::fromJSON(message)
print(input_message)
cat("Number of bins:", input_message$value, "\n")
# Create a plot in the server
hist(rnorm(input_message$value))
if (!is.null(delay)) Sys.sleep(delay)
# Create a message to update gauge widget in client side
output_message <- jsonlite::toJSON(
list(
val = sample(0:100, 1),
message = "Thanks client! I updated the plot..."
),
pretty = TRUE,
auto_unbox = TRUE
)
# Sent a message
ws$send(output_message)
cat(output_message)
})
# Confirm that the server have been closed
ws$onClose(function() {
cat("Server connection closed.\n")
})
}