Skip to content

Instantly share code, notes, and snippets.

@sandys
Forked from MattSandy/server.R
Created November 30, 2015 20:01
Show Gist options
  • Save sandys/2c5a70b95694f675fda4 to your computer and use it in GitHub Desktop.
Save sandys/2c5a70b95694f675fda4 to your computer and use it in GitHub Desktop.
Creates a websocket based JSON server
#Connect to this using websockets on port 9454
#Send in the format of {"data":[1,2,3]}
#The ppp returns the standard deviation of the sent array
library(jsonlite)
library(httpuv)
#server
app <- list(
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {
write(message, file = "log.txt",append = TRUE, sep = "\n")
message <- fromJSON(message)
ws$send(toJSON(sd(message$data)))
})
}
)
runServer("0.0.0.0", 9454, app, 250)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment