Interface ServerSentEventServer
- All Superinterfaces:
AutoCloseable
A Soklet application which supports Server-Sent Events will be configured with both a Server and a ServerSentEventServer.
For example:
// Set up our HTTP and SSE servers
Server server = Server.withPort(8080).build;
ServerSentEventServer sseServer = ServerSentEventServer.withPort(8081).build();
// Wire servers into our config
SokletConfig config = SokletConfig.withServer(server)
.serverSentEventServer(sseServer)
.build();
// Run the app
try (Soklet soklet = Soklet.withConfig(config)) {
soklet.start();
System.out.println("Soklet started, press [enter] to exit");
soklet.awaitShutdown(ShutdownTrigger.ENTER_KEY);
}
See https://www.soklet.com/docs/server-sent-events for detailed documentation.
- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classBuilder used to construct a standard implementation ofServerSentEventServer.static interfaceRequest/response processing contract forServerSentEventServerimplementations. -
Method Summary
Modifier and TypeMethodDescriptionOptional<? extends ServerSentEventBroadcaster> acquireBroadcaster(ResourcePath resourcePath) Given aResourcePaththat corresponds to a Resource Method annotated withServerSentEventSource, acquire aServerSentEventBroadcasterwhich is capable of "pushing" messages to all connected Server-Sent Event clients.default voidclose()AutoCloseable-enabled synonym forstop().voidinitialize(SokletConfig sokletConfig, ServerSentEventServer.RequestHandler requestHandler) TheSokletinstance which manages thisServerSentEventServerwill invoke this method exactly once at initialization time - this allowsSokletto "talk" to yourServerSentEventServer.Is this SSE server started (that is, able to handle requests from clients)?voidstart()Starts the SSE server, which makes it able to accept requests from clients.voidstop()Stops the SSE server, which makes it unable to accept requests from clients.Acquires a builder forServerSentEventServerinstances.
-
Method Details
-
start
-
stop
-
isStarted
-
close
AutoCloseable-enabled synonym forstop().This method is designed for internal use by
Sokletonly and should not be invoked elsewhere.- Specified by:
closein interfaceAutoCloseable- Throws:
Exception- if an exception occurs while stopping the server
-
acquireBroadcaster
@Nonnull Optional<? extends ServerSentEventBroadcaster> acquireBroadcaster(@Nullable ResourcePath resourcePath) Given aResourcePaththat corresponds to a Resource Method annotated withServerSentEventSource, acquire aServerSentEventBroadcasterwhich is capable of "pushing" messages to all connected Server-Sent Event clients.When using the default
ServerSentEventServer, Soklet guarantees exactly oneServerSentEventBroadcasterinstance exists perResourcePath(within the same JVM process). Soklet is responsible for the creation and management ofServerSentEventBroadcasterinstances.Your code should not hold long-lived references to
ServerSentEventBroadcasterinstances (e.g. in a cache or instance variables) - the recommended usage pattern is to invokeacquireBroadcaster(ResourcePath)every time you need a broadcaster reference.See https://www.soklet.com/docs/server-sent-events for detailed documentation.
- Parameters:
resourcePath- theServerSentEventSource-annotated Resource Method for which to acquire a broadcaster- Returns:
- a broadcaster for the given
ResourcePath, orOptional.empty()if there is no broadcaster available
-
initialize
void initialize(@Nonnull SokletConfig sokletConfig, @Nonnull ServerSentEventServer.RequestHandler requestHandler) TheSokletinstance which manages thisServerSentEventServerwill invoke this method exactly once at initialization time - this allowsSokletto "talk" to yourServerSentEventServer.This method is designed for internal use by
Sokletonly and should not be invoked elsewhere.- Parameters:
sokletConfig- configuration for the Soklet instance that controls this serverrequestHandler- aSoklet-internal request handler which takes aServerSentEventServer-provided request as input and supplies aMarshaledResponseas output for theServerSentEventServerto write back to the client
-
withPort
Acquires a builder forServerSentEventServerinstances.- Parameters:
port- the port number on which the server should listen- Returns:
- the builder
-