Skip to content

Commit

Permalink
fix: custom websocket use closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Apr 7, 2024
1 parent 0aba170 commit 42055ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ License: GPL-3
Encoding: UTF-8
LazyData: false
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Depends: R (>= 4.1.0)
Imports:
fs,
Expand Down
2 changes: 1 addition & 1 deletion R/ambiorix.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Ambiorix <- R6::R6Class(
app = list(
call = super$.call,
staticPaths = private$.static,
onWSOpen = super$.wss,
onWSOpen = super$websocket,
staticPathOptions = httpuv::staticPathOptions(
html_charset = "utf-8",
headers = list(
Expand Down
24 changes: 18 additions & 6 deletions R/routing.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Routing <- R6::R6Class(

# recurse through items
if(is.list(use)) {
for(i in 1:length(use)) {
for(i in seq_along(use)) {
self$use(use[[i]])
}
}
Expand Down Expand Up @@ -325,6 +325,18 @@ Routing <- R6::R6Class(
return(private$.middleware)
}
),
active = list(
websocket = function(ws){
if(missing(ws) && !is.null(private$.wss_custom))
return(private$.wss_custom)

if(missing(ws) && is.null(private$.wss_custom))
return(private$.wss)

private$.wss_custom <- ws
invisible(self)
}
),
private = list(
.basepath = "/",
.is_router = FALSE,
Expand All @@ -333,6 +345,7 @@ Routing <- R6::R6Class(
.receivers = list(),
.middleware = list(),
.is_running = FALSE,
.wss_custom = NULL,
# we reorder the routes before launching the app
# we make sure the longest patterns are checked first
# this makes sure /:id/x matches BEFORE /:id does
Expand All @@ -344,7 +357,7 @@ Routing <- R6::R6Class(
if(length(private$.routes) < 3L)
return()

indices <- 1:length(private$.routes)
indices <- seq_along(private$.routes)
pats <- lapply(private$.routes, \(route) {
data.frame(
pattern = route$route$pattern,
Expand All @@ -369,7 +382,7 @@ Routing <- R6::R6Class(
res <- Response$new()

if(length(private$.middleware) > 0L){
for(i in 1:length(private$.middleware)) {
for(i in seq_along(private$.middleware)) {
mid_basepath <- attr(private$.middleware[[i]], "basepath")

mid_res <- NULL
Expand All @@ -382,7 +395,7 @@ Routing <- R6::R6Class(
}

# loop over routes
for(i in 1:length(private$.routes)){
for(i in seq_along(private$.routes)){
# if path matches pattern and method
if(grepl(private$.routes[[i]]$route$pattern, req$PATH_INFO) && req$REQUEST_METHOD %in% private$.routes[[i]]$method){

Expand Down Expand Up @@ -450,7 +463,7 @@ Routing <- R6::R6Class(

message <- jsonlite::fromJSON(message)

for(i in 1:length(private$.receivers)){
for(i in seq_along(private$.receivers)){
if(private$.receivers[[i]]$is_handler(message)){
.globals$infoLog$log("Received message from websocket:", message$name)
return(private$.receivers[[i]]$receive(message, ws))
Expand All @@ -467,4 +480,3 @@ Routing <- R6::R6Class(
}
)
)

0 comments on commit 42055ea

Please sign in to comment.