Learn R Programming

websockets (version 1.1.7)

create_server: Create a websocket server.

Description

Create a websocket server and bind it to a port.

Usage

create_server(port = 7681L, webpage = static_file_service(paste(system.file(package = "websockets"), "basic.html", sep = "//")), is.binary = FALSE)
createContext(port = 7681L, webpage = static_file_service(paste(system.file(package = "websockets"), "basic.html", sep = "//")), server = TRUE, is.binary = FALSE)

Arguments

port

The service port number.

webpage

Optionally, specifiy a function to service HTTP requests. This value may be NULL.

server

Set FALSE if the environment will represent a client connection (used internally by the websocket function.

is.binary

The websocket connection binary transport flag.

Value

An environment is returned. The R/Websocket service will search the environment for callback functions named: closed, established, and receive. The established and closed functions must take a single argument (a websocket). The receive function must take three arguments: DATA, WS, and '...', holding the message data, websocket client, and possible ancillary information received. The callback functions are optional. When a websocket event occurs, the corresponding function will be evaluated if it exists. The DATA parameter holds a RAW vector with incoming data from broadcast or receive events, and may be of length zero for other events. The WS parameter is the client web socket corresponding to the event.

See the set_callback function for more information.

Details

The websockets package supports an arbitrary number of servers, each listening on a distinct TCP port number. An environment is associated with each running server. Server events must be serviced with the service function.

The webpage callback must be a function of two arguments, socket and header. The socket argument indicates the low-level socket descriptor of the client. The header argument contains a parsed HTTP request header. When invoked, the function may examine the request and respond using the http_response and http_vars functions.

See Also

service, websocket_broadcast websocket_write set_callback http_response http_vars

Examples

Run this code
# NOT RUN {
# See the detailed example in help('websockets').
# }

Run the code above in your browser using DataLab