Search in sources :

Example 1 with ServerSentEventConnection

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();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) StringReadChannelListener(io.undertow.util.StringReadChannelListener) ServerSentEventConnection(io.undertow.server.handlers.sse.ServerSentEventConnection) ServerSentEventHandler(io.undertow.server.handlers.sse.ServerSentEventHandler) IOException(java.io.IOException) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) Undertow(io.undertow.Undertow)

Aggregations

Undertow (io.undertow.Undertow)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)1 ServerSentEventConnection (io.undertow.server.handlers.sse.ServerSentEventConnection)1 ServerSentEventHandler (io.undertow.server.handlers.sse.ServerSentEventHandler)1 StringReadChannelListener (io.undertow.util.StringReadChannelListener)1 IOException (java.io.IOException)1