This is an R WebSocket client library backed by the websocketpp C++ library.
You may need to step through the $send()
commands because there will be a small amount of time before the response is received. The onMessage()
callback is invoked asynchronously (using the later package) when a response arrives.
library(websocket)
ws <- WebsocketClient$new("ws://echo.websocket.org/",
onOpen = function() {
cat("Connection opened\n")
},
onMessage = function(msg) {
cat("Client got msg: ", msg, "\n")
},
onClose = function() {
cat("Client disconnected\n")
}
)
ws$send("hello")
ws$send( charToRaw("hello") )
ws$close()
websocket supports ws:// and wss:// URLs.
Currently we do local development by running a simple httpuv-backed WebSocket server written in R that lives at tmp/websocketServer.R
.
To run it, you need to install the Github version of httpuv
.
Note: If you want httpuv to build faster, you can create a file at ~/.Renviron with the following content:
MAKEFLAGS=-j4
To install the development version of httpuv, run:
remotes::install_github("rstudio/httpuv")`
Then to run the WebSocket server, run this in a terminal:
R -e 'source("tmp/websocketServer.R"); httpuv::service(Inf)'
Finally, to test the WebSocket client, run this in R.
library(websocket)
ws <- WebsocketClient$new("ws://127.0.0.1:8080/",
headers = list(Cookie = "Xyz"),
onOpen = function() {
cat("Connection opened.\n")
},
onMessage = function(msg) {
cat("Client received message: ", msg, "\n")
},
onClose = function() {
cat("Connection closed.\n")
},
accessLogChannels = "all" # enable all websocketpp logging
)
ws$send("hello")
ws$send( charToRaw("hello") )
ws$close()