| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/server.R |
| 3 |
\name{handler_ws} |
| 4 |
\alias{handler_ws} |
| 5 |
\title{Create WebSocket Handler} |
| 6 |
\usage{ |
| 7 |
handler_ws( |
| 8 |
path, |
| 9 |
on_message, |
| 10 |
on_open = NULL, |
| 11 |
on_close = NULL, |
| 12 |
textframes = FALSE |
| 13 |
) |
| 14 |
} |
| 15 |
\arguments{ |
| 16 |
\item{path}{URI path for WebSocket connections (e.g., "/ws").} |
| 17 |
|
| 18 |
\item{on_message}{Function called when a message is received. |
| 19 |
Signature: \verb{function(ws, data)} where \code{ws} is the connection |
| 20 |
object and \code{data} is the message. Use \code{ws$send()} to send |
| 21 |
responses; the return value is ignored.} |
| 22 |
|
| 23 |
\item{on_open}{[default NULL] Function called when a connection opens. |
| 24 |
Signature: \verb{function(ws, req)} where \code{req} is a list with \code{uri} |
| 25 |
(character) and \code{headers} (named character vector) from the HTTP upgrade |
| 26 |
request.} |
| 27 |
|
| 28 |
\item{on_close}{[default NULL] Function called when a connection closes. |
| 29 |
Signature: \verb{function(ws)}} |
| 30 |
|
| 31 |
\item{textframes}{[default FALSE] Logical, use text frames instead of binary. |
| 32 |
When TRUE: incoming \code{data} is character, outgoing data should be character. |
| 33 |
When FALSE: incoming \code{data} is raw vector, outgoing data should be raw vector.} |
| 34 |
} |
| 35 |
\value{ |
| 36 |
A handler object for use with \code{\link[=http_server]{http_server()}}. |
| 37 |
} |
| 38 |
\description{ |
| 39 |
Creates a WebSocket handler for use with \code{\link[=http_server]{http_server()}}. |
| 40 |
} |
| 41 |
\section{Connection Object}{ |
| 42 |
|
| 43 |
The \code{ws} object passed to callbacks has the following fields and methods: |
| 44 |
\itemize{ |
| 45 |
\item \code{ws$send(data)}: Send a message to the client. \code{data} can be |
| 46 |
a raw vector or character string. Returns 0 on success, or an error code |
| 47 |
on failure (e.g., if the connection is closed). |
| 48 |
\item \code{ws$close()}: Close the connection. |
| 49 |
\item \code{ws$id}: Unique integer identifier for this connection. No two |
| 50 |
connections on the same server will share an ID, even across different |
| 51 |
handlers, making IDs safe to use as keys in a shared data structure. |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
\examples{ |
| 56 |
# Simple echo server |
| 57 |
h <- handler_ws("/ws", function(ws, data) ws$send(data)) |
| 58 |
|
| 59 |
# With connection tracking |
| 60 |
clients <- list() |
| 61 |
h <- handler_ws( |
| 62 |
"/chat", |
| 63 |
on_message = function(ws, data) { |
| 64 |
# Broadcast to all |
| 65 |
for (client in clients) client$send(data) |
| 66 |
}, |
| 67 |
on_open = function(ws, req) { |
| 68 |
clients[[as.character(ws$id)]] <<- ws |
| 69 |
}, |
| 70 |
on_close = function(ws) { |
| 71 |
clients[[as.character(ws$id)]] <<- NULL |
| 72 |
}, |
| 73 |
textframes = TRUE |
| 74 |
) |
| 75 |
|
| 76 |
} |