Skip to content

Commit

Permalink
Add runServer convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Feb 26, 2013
1 parent a0267ad commit b715e65
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export(runServer)
export(service)
export(startServer)
export(stopServer)
Expand Down
36 changes: 35 additions & 1 deletion R/eventloop.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ WebSocket <- setRefClass(
#' 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 \code{\link{WebSocket}}.}
#' }
#'
#' @seealso \code{\link{runServer}}
#' @export
startServer <- function(host, port, app) {

Expand Down Expand Up @@ -247,3 +247,37 @@ service <- function(timeoutMs = ifelse(interactive(), 100, 1000)) {
stopServer <- function(handle) {
destroyServer(handle)
}

#' Run a server
#'
#' This is a convenience function that provides a simple way to call
#' \code{\link{startServer}}, \code{\link{service}}, and
#' \code{\link{stopServer}} in the correct sequence. It does not return unless
#' interrupted or an error occurs.
#'
#' If you have multiple hosts and/or ports to listen on, call the individual
#' functions instead of \code{runServer}.
#'
#' @param host A string that is a valid IPv4 address that is owned by this
#' server, or \code{"0.0.0.0"} to listen on all IP addresses.
#' @param 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.
#' @param app A collection of functions that define your application. See
#' Details.
#' @param interruptIntervalMs How often to check for interrupt. The default
#' should be appropriate for most situations.
#'
#' @seealso \code{\link{startServer}}, \code{\link{service}},
#' \code{\link{stopServer}}
#' @export
runServer <- function(host, port, app,
interruptIntervalMs = ifelse(interactive(), 100, 1000)) {
server <- startServer(host, port, app)
on.exit(stopServer(server))

while (TRUE) {
service(interruptIntervalMs)
Sys.sleep(0.001)
}
}
10 changes: 2 additions & 8 deletions demo/echo.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ app <- list(
sprintf("var ws = new WebSocket(%s);", wsUrl),
"ws.onmessage = function(msg) {",
' var msgDiv = document.createElement("pre");',
' msgDiv.innerHTML = msg.data;',
' msgDiv.innerHTML = msg.data.replace(/&/g, "&amp;").replace(/\\</g, "&lt;");',
' document.getElementById("output").appendChild(msgDiv);',
"}",
"function sendInput() {",
Expand Down Expand Up @@ -55,11 +55,5 @@ app <- list(
}
)

server <- startServer("0.0.0.0", 9454, app, 250)
browseURL("http://localhost:9454/")
tryCatch({
while (TRUE) {
service()
Sys.sleep(0.1)
}
}, finally = stopServer(server))
runServer("0.0.0.0", 9454, app, 250)
40 changes: 40 additions & 0 deletions man/runServer.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
\name{runServer}
\alias{runServer}
\title{Run a server}
\usage{
runServer(host, port, app,
interruptIntervalMs = ifelse(interactive(), 100, 1000))
}
\arguments{
\item{host}{A string that is a valid IPv4 address that is
owned by this server, or \code{"0.0.0.0"} to listen on
all IP addresses.}

\item{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.}

\item{app}{A collection of functions that define your
application. See Details.}

\item{interruptIntervalMs}{How often to check for
interrupt. The default should be appropriate for most
situations.}
}
\description{
This is a convenience function that provides a simple way
to call \code{\link{startServer}}, \code{\link{service}},
and \code{\link{stopServer}} in the correct sequence. It
does not return unless interrupted or an error occurs.
}
\details{
If you have multiple hosts and/or ports to listen on,
call the individual functions instead of
\code{runServer}.
}
\seealso{
\code{\link{startServer}}, \code{\link{service}},
\code{\link{stopServer}}
}

0 comments on commit b715e65

Please sign in to comment.