Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws experiment #724

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ Collate:
'utils-pipe.R'
'utils.R'
'validate_api_spec.R'
'websocket.R'
'zzz.R'
RdMacros: lifecycle
14 changes: 12 additions & 2 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Plumber <- R6Class(
self$setDocsCallback(getOption('plumber.docs.callback', getOption('plumber.swagger.url', NULL)))
self$setDebug(interactive())
self$setApiSpec(NULL)
self$websocket(defaultWebsocket(self, private$default_serializer))

# Add in the initial filters
for (fn in names(filters)){
Expand Down Expand Up @@ -755,7 +756,10 @@ Plumber <- R6Class(
#' @description httpuv interface onWSOpen function. (Required for \pkg{httpuv})
#' @param ws WebSocket object
onWSOpen = function(ws){
warning("WebSockets not supported.")
if (!is.null(private$ws_open)) {
private$ws_open(ws)
}
invisible(self)
},
#' @description Sets the default serializer of the router.
#'
Expand Down Expand Up @@ -923,7 +927,12 @@ Plumber <- R6Class(

ret
},

#' @description Set websocket open method
#' @param open on open websocket method
websocket = function(open = NULL) {
if (!is.null(open)) stopifnot(is.function(open))
private$ws_open <- open
},

### Legacy/Deprecated
#' @description addEndpoint has been deprecated in v0.4.0 and will be removed in a coming release. Please use `handle()` instead.
Expand Down Expand Up @@ -1074,6 +1083,7 @@ Plumber <- R6Class(
docs_info = NULL,
docs_callback = NULL,
debug = NULL,
ws_open = NULL,

addFilterInternal = function(filter){
# Create a new filter and add it to the router
Expand Down
27 changes: 27 additions & 0 deletions R/websocket.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' @noRd
defaultWebsocket <- function(pr, ser) {
function(ws) {
req <- ws$request
req$ws <- ws
req$pr <- pr
ws$onMessage(function(binary, message) {
req$.internal <- new.env()
req$args <- list()
req$bodyRaw <- message
delayedAssign(
"postBody",
{
if (binary) rawToChar(message) else message
},
assign.env = req
)
req$.internal$bodyHandled <- TRUE
res <- PlumberResponse$new(ser)
pr$serve(req, res)
ws$send(paste("_status_", res$status))
ws$send(paste("_headers_", paste(names(res$headers), unlist(res$headers), sep = "=", collapse = ";")))
ws$send("_body_ nextmessage")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably makes more sense to only return res$body in websockets context?

ws$send(res$body)
})
}
}
18 changes: 18 additions & 0 deletions man/Plumber.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/PlumberStatic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/deprecated_r6.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.