use of io.undertow.server.handlers.sse.ServerSentEventConnection in project undertow by undertow-io.
the class ServerSentEventsServer method main.
public static void main(final String[] args) {
final ServerSentEventHandler sseHandler = serverSentEvents();
HttpHandler chatHandler = new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
new StringReadChannelListener(exchange.getConnection().getByteBufferPool()) {
@Override
protected void stringDone(String string) {
for (ServerSentEventConnection h : sseHandler.getConnections()) {
h.send(string);
}
}
@Override
protected void error(IOException e) {
}
}.setup(exchange.getRequestChannel());
}
};
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/sse", sseHandler).addPrefixPath("/send", chatHandler).addPrefixPath("/", resource(new ClassPathResourceManager(ServerSentEventsServer.class.getClassLoader(), ServerSentEventsServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
server.start();
}
Aggregations