在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:rstudio/httpuv开源软件地址:https://github.com/rstudio/httpuv开源编程语言:C 67.2%开源软件介绍:httpuv: HTTP and WebSocket server library for Rhttpuv provides low-level socket and protocol support for handling HTTP and WebSocket requests directly from within R. It uses a multithreaded architecture, where I/O is handled on one thread, and the R callbacks are handled on another. It is primarily intended as a building block for other packages, rather than making it particularly easy to create complete web applications using httpuv alone. httpuv is built on top of the libuv and http-parser C libraries, both of which were developed by Joyent, Inc. InstallingYou can install the stable version from CRAN, or the development version using remotes: # install from CRAN
install.packages("httpuv")
# or if you want to test the development version here
if (!require("remotes")) install.packages("remotes")
remotes::install_github("rstudio/httpuv") Since httpuv contains C code, you'll need to make sure you're set up to install packages with compiled code. Follow the instructions at http://www.rstudio.com/ide/docs/packages/prerequisites Basic UsageThis is a basic web server that listens on port 8080 and responds to HTTP requests with a web page containing the current system time and the path of the request: library(httpuv)
s <- startServer(host = "0.0.0.0", port = 8080,
app = list(
call = function(req) {
body <- paste0("Time: ", Sys.time(), "<br>Path requested: ", req$PATH_INFO)
list(
status = 200L,
headers = list('Content-Type' = 'text/html'),
body = body
)
}
)
) Note that when The The To stop the server: s$stop() Or, to stop all running httpuv servers: stopAllServers() Static pathsA httpuv server application can serve up files on disk. This happens entirely within the I/O thread, so doing so will not block or be blocked by activity in the main R thread. To serve a path, use s <- startServer("0.0.0.0", 8080,
app = list(
staticPaths = list("/" = "www/")
)
) By default, if a file named
s <- startServer("0.0.0.0", 8080,
list(
call = function(req) {
list(
status = 200L,
headers = list(
'Content-Type' = 'text/html'
),
body = "Hello world!"
)
},
staticPaths = list(
"/assets" = "content/assets/",
# Don't use index.html for /lib
"/lib" = staticPath("content/lib", indexhtml = FALSE)
)
)
) WebSocket serverhttpuv also can handle WebSocket connections. For example, this app acts as a WebSocket echo server: s <- startServer("127.0.0.1", 8080,
list(
onWSOpen = function(ws) {
# The ws object is a WebSocket object
cat("Server connection opened.\n")
ws$onMessage(function(binary, message) {
cat("Server received message:", message, "\n")
ws$send(message)
})
ws$onClose(function() {
cat("Server connection closed.\n")
})
}
)
) To test it out, you can connect to it using the websocket package (which provides a WebSocket client). You can do this from the same R process or a different one. ws <- websocket::WebSocket$new("ws://127.0.0.1:8080/")
ws$onMessage(function(event) {
cat("Client received message:", event$data, "\n")
})
# Wait for a moment before running next line
ws$send("hello world")
# Close client
ws$close() Note that both the httpuv and websocket packages provide a class named Debugging buildshttpuv can be built with debugging options enabled. This can be done by uncommenting these lines in src/Makevars, and then installing. The first one enables thread assertions, to ensure that code is running on the correct thread; if not. The second one enables tracing statements: httpuv will print lots of messages when various events occur.
To install it directly from GitHub with these options, you can use withr::with_makevars(
c(PKG_CPPFLAGS="-DDEBUG_TRACE -DDEBUG_THREAD -UNDEBUG"), {
devtools::install_github("rstudio/httpuv")
}, assignment = "+="
) © 2013-2020 RStudio, Inc. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论