Learn R Programming

websockets (version 1.1.7)

websocket: Create a websocket client.

Description

Create a websocket client connection.

Usage

websocket(url, port=80, subprotocol="chat", version=0)

Arguments

url

The websocket url to connect to.

port

The service port number.

subprotocol

A websocket subprotocol id.

version

A websocket protocol version.

Value

An environment is returned. The 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 HEADER, holding the message data, websocket client, and possible ancillary header 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

Create a context for a websocket client connection. All the functions available to a websocket server context, including callbacks for handling events for the connection, may be defined just as with a server context created with create_server.

The websocket_write function can also be used to write data through the websocket client socket to the service.

See Also

service, websocket_write set_callback

Examples

Run this code
# NOT RUN {
# Connect to the public websocket echo service
# }
# NOT RUN {
client = websocket("ws://echo.websocket.org")
set_callback("receive", function(DATA, WS, HEADER) cat(rawToChar(DATA),"\n"), client)
websocket_write("Testing, testing", client)
service(client)
websocket_close(client)
# }

Run the code above in your browser using DataLab