Last data update: 2014.03.03

R: Create an HTTP/WebSocket server
startServerR Documentation

Create an HTTP/WebSocket server

Description

Creates an HTTP/WebSocket server on the specified host and port.

Usage

startServer(host, port, app)

startPipeServer(name, mask, app)

Arguments

host

A string that is a valid IPv4 address that is owned by this server, or "0.0.0.0" to listen on all IP addresses.

port

A number or integer that indicates the server port that should be listened on. Note that on most Unix-like systems including Linux and Mac OS X, port numbers smaller than 1025 require root privileges.

app

A collection of functions that define your application. See Details.

name

A string that indicates the path for the domain socket (on Unix-like systems) or the name of the named pipe (on Windows).

mask

If non-NULL and non-negative, this numeric value is used to temporarily modify the process's umask while the domain socket is being created. To ensure that only root can access the domain socket, use strtoi("777", 8); or to allow owner and group read/write access, use strtoi("117", 8). If the value is NULL then the process's umask is left unchanged. (This parameter has no effect on Windows.)

Details

startServer binds the specified port, but no connections are actually accepted. See service, which should be called repeatedly in order to actually accept and handle connections. If the port cannot be bound (most likely due to permissions or because it is already bound), an error is raised.

The app parameter is where your application logic will be provided to the server. This can be a list, environment, or reference class that contains the following named functions/methods:

call(req)

Process the given HTTP request, and return an HTTP response. This method should be implemented in accordance with the Rook specification.

onHeaders(req)

Optional. Similar to call, but occurs when headers are received. Return NULL to continue normal processing of the request, or a Rook response to send that response, stop processing the request, and ask the client to close the connection. (This can be used to implement upload size limits, for example.)

onWSOpen(ws)

Called back when a WebSocket connection is established. The given object can be used to be notified when a message is received from the client, to send messages to the client, etc. See WebSocket.

The startPipeServer variant can be used instead of startServer to listen on a Unix domain socket or named pipe rather than a TCP socket (this is not common).

Value

A handle for this server that can be passed to stopServer to shut the server down.

See Also

runServer

Results