A WebSocket object represents a single WebSocket connection. The
object can be used to send messages and close the connection, and to receive
notifications when messages are received or the connection is closed.
Details
Note that this WebSocket class is different from the one provided by the package named websocket. This class is meant to be used on the server side, whereas the one in the websocket package is to be used as a client. The WebSocket class in httpuv has an older API than the one in the websocket package.
WebSocket objects should never be created directly. They are obtained by
passing an onWSOpen function to startServer().
Public fields
handleThe server handle
messageCallbacksA list of callback functions that will be invoked when a message is received on this connection.
closeCallbacksA list of callback functions that will be invoked when the connection is closed.
requestThe Rook request environment that opened the connection. This can be used to inspect HTTP headers, for example.
Methods
Method onMessage()
Registers a callback function that will be invoked whenever a message is received on this connection.
Method close()
Closes the websocket connection
Arguments
codeAn integer that indicates the WebSocket close code.
reasonA concise human-readable prose explanation for the closure.
Examples
if (FALSE) { # \dontrun{
# A WebSocket echo server that listens on port 8080
startServer("0.0.0.0", 8080,
list(
onHeaders = function(req) {
# Print connection headers
cat(capture.output(str(as.list(req))), sep = "\n")
},
onWSOpen = function(ws) {
cat("Connection opened.\n")
ws$onMessage(function(binary, message) {
cat("Server received message:", message, "\n")
ws$send(message)
})
ws$onClose(function() {
cat("Connection closed.\n")
})
}
)
)
} # }