public abstract class WebSockets extends Object
An example that broadcasts strings to a websocket every second:
import org.reactivestreams.Publisher;
import ratpack.test.embed.EmbeddedApp;
import ratpack.websocket.WebSockets;
import java.time.Duration;
import static ratpack.stream.Streams.periodically;
chain.get("whatever", context -> {
Publisher<String> stream = periodically(context, Duration.ofSeconds(1), i ->
i < 5 ? i.toString() : null
);
WebSockets.websocketBroadcast(context, stream);
});
Constructor and Description |
---|
WebSockets() |
Modifier and Type | Method and Description |
---|---|
static <T> WebSocketConnector<T> |
websocket(Context context,
Function<WebSocket,T> openAction) |
static void |
websocket(Context context,
WebSocketHandler<?> handler) |
static void |
websocketBroadcast(Context context,
Publisher<String> broadcaster)
Sets up a websocket that sends the published Strings to a client.
|
static void |
websocketByteBufBroadcast(Context context,
Publisher<ByteBuf> broadcaster)
Sets up a websocket that sends the published byte buffers to a client.
|
public static <T> WebSocketConnector<T> websocket(Context context, Function<WebSocket,T> openAction)
public static void websocket(Context context, WebSocketHandler<?> handler)
public static void websocketBroadcast(Context context, Publisher<String> broadcaster)
This takes the place of a Streams.bindExec(Publisher)
call.
context
- the request handling contextbroadcaster
- a Publisher
of Strings to send to the websocket clientpublic static void websocketByteBufBroadcast(Context context, Publisher<ByteBuf> broadcaster)
This takes the place of a Streams.bindExec(Publisher)
call.