Interface SseServer
- All Superinterfaces:
AutoCloseable
A Soklet application which supports Server-Sent Events will be configured with a SseServer.
A regular HttpServer is only required if the same application also serves ordinary HTTP Resource Methods.
For example:
// Set up our SSE server
SseServer sseServer = SseServer.fromPort(8081);
// Wire the SSE server into our config
SokletConfig config = SokletConfig.withSseServer(sseServer)
.build();
// Add .httpServer(HttpServer.fromPort(8080)) if you also serve ordinary HTTP resources
// Run the app
try (Soklet soklet = Soklet.fromConfig(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 Classes -
Method Summary
Modifier and TypeMethodDescription@NonNull Optional<? extends SseBroadcaster> acquireBroadcaster(@Nullable ResourcePath resourcePath) Given aResourcePaththat corresponds to a Resource Method annotated withSseEventSource, acquire aSseBroadcasterwhich is capable of "pushing" messages to all connected Server-Sent Event clients.default voidclose()AutoCloseable-enabled synonym forstop().Creates aSseServerconfigured with the given port and default settings.voidinitialize(@NonNull SokletConfig sokletConfig, @NonNull SseServer.RequestHandler requestHandler) 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.static @NonNull SseServer.BuilderAcquires a builder forSseServerinstances.
-
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
Given aResourcePaththat corresponds to a Resource Method annotated withSseEventSource, acquire aSseBroadcasterwhich is capable of "pushing" messages to all connected Server-Sent Event clients.When using the default
SseServer, Soklet guarantees exactly oneSseBroadcasterinstance exists perResourcePath(within the same JVM process). Soklet is responsible for the creation and management ofSseBroadcasterinstances.Your code should not hold long-lived references to
SseBroadcasterinstances (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- theSseEventSource-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 SseServer.RequestHandler requestHandler) TheSokletinstance which manages thisSseServerwill invoke this method exactly once at initialization time - this allowsSokletto "talk" to yourSseServer.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 aSseServer-provided request as input and supplies aMarshaledResponseas output for theSseServerto write back to the client
-
withPort
-
fromPort
-