Search in sources :

Example 1 with ServerSentEventHandler

use of io.undertow.server.handlers.sse.ServerSentEventHandler 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)

Example 2 with ServerSentEventHandler

use of io.undertow.server.handlers.sse.ServerSentEventHandler in project undertow by undertow-io.

the class ServerSentEventSCI method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
    if (c == null || c.isEmpty()) {
        return;
    }
    try {
        final Map<String, ServerSentEventConnectionCallback> callbacks = new HashMap<>();
        ServletContextImpl servletContext = (ServletContextImpl) ctx;
        final List<InstanceHandle<?>> handles = new ArrayList<>();
        for (Class<?> clazz : c) {
            final ServerSentEvent annotation = clazz.getAnnotation(ServerSentEvent.class);
            if (annotation == null) {
                continue;
            }
            String path = annotation.value();
            final InstanceHandle<?> instance = servletContext.getDeployment().getDeploymentInfo().getClassIntrospecter().createInstanceFactory(clazz).createInstance();
            handles.add(instance);
            callbacks.put(path, (ServerSentEventConnectionCallback) instance.getInstance());
        }
        if (callbacks.isEmpty()) {
            return;
        }
        servletContext.getDeployment().getDeploymentInfo().addInnerHandlerChainWrapper(new HandlerWrapper() {

            @Override
            public HttpHandler wrap(HttpHandler handler) {
                PathTemplateHandler pathTemplateHandler = new PathTemplateHandler(handler, false);
                for (Map.Entry<String, ServerSentEventConnectionCallback> e : callbacks.entrySet()) {
                    pathTemplateHandler.add(e.getKey(), new ServerSentEventHandler(e.getValue()));
                }
                return pathTemplateHandler;
            }
        });
        servletContext.addListener(new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent sce) {
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                for (InstanceHandle<?> h : handles) {
                    h.release();
                }
            }
        });
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) HashMap(java.util.HashMap) ServletContextListener(javax.servlet.ServletContextListener) ServerSentEventConnectionCallback(io.undertow.server.handlers.sse.ServerSentEventConnectionCallback) PathTemplateHandler(io.undertow.server.handlers.PathTemplateHandler) ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) ArrayList(java.util.ArrayList) ServerSentEventHandler(io.undertow.server.handlers.sse.ServerSentEventHandler) HandlerWrapper(io.undertow.server.HandlerWrapper) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) InstanceHandle(io.undertow.servlet.api.InstanceHandle) ServletContextEvent(javax.servlet.ServletContextEvent)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)2 ServerSentEventHandler (io.undertow.server.handlers.sse.ServerSentEventHandler)2 Undertow (io.undertow.Undertow)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 PathTemplateHandler (io.undertow.server.handlers.PathTemplateHandler)1 ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)1 ServerSentEventConnection (io.undertow.server.handlers.sse.ServerSentEventConnection)1 ServerSentEventConnectionCallback (io.undertow.server.handlers.sse.ServerSentEventConnectionCallback)1 InstanceHandle (io.undertow.servlet.api.InstanceHandle)1 ServletContextImpl (io.undertow.servlet.spec.ServletContextImpl)1 StringReadChannelListener (io.undertow.util.StringReadChannelListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletException (javax.servlet.ServletException)1