Search in sources :

Example 11 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager 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 12 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project undertow by undertow-io.

the class WebSocketServer method main.

public static void main(final String[] args) {
    // Demonstrates how to use Websocket Protocol Handshake to enable Per-message deflate
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
                    WebSockets.sendText(message.getData(), channel, null);
                }
            });
            channel.resumeReceives();
        }
    }).addExtension(new PerMessageDeflateHandshake(false, 6))).addPrefixPath("/", resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(), WebSocketServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
    server.start();
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Undertow(io.undertow.Undertow) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage)

Example 13 with ClassPathResourceManager

use of io.undertow.server.handlers.resource.ClassPathResourceManager in project undertow by undertow-io.

the class ChatServer method main.

public static void main(final String[] args) {
    System.out.println("To see chat in action is to open two different browsers and point them at http://localhost:8080");
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(path().addPrefixPath("/myapp", websocket(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
                    final String messageData = message.getData();
                    for (WebSocketChannel session : channel.getPeerConnections()) {
                        WebSockets.sendText(messageData, session, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    })).addPrefixPath("/", resource(new ClassPathResourceManager(ChatServer.class.getClassLoader(), ChatServer.class.getPackage())).addWelcomeFiles("index.html"))).build();
    server.start();
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ClassPathResourceManager(io.undertow.server.handlers.resource.ClassPathResourceManager) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Undertow(io.undertow.Undertow) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage)

Aggregations

ClassPathResourceManager (io.undertow.server.handlers.resource.ClassPathResourceManager)13 Undertow (io.undertow.Undertow)10 PathHandler (io.undertow.server.handlers.PathHandler)5 HttpHandler (io.undertow.server.HttpHandler)4 DeploymentManager (io.undertow.servlet.api.DeploymentManager)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)3 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)3 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)3 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)3 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)3 URI (java.net.URI)2 ReturnException (org.matheclipse.core.eval.exception.ReturnException)2 ConfigureListener (com.sun.faces.config.ConfigureListener)1 Config (com.typesafe.config.Config)1 Predicate (io.undertow.predicate.Predicate)1 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 DirectBufferCache (io.undertow.server.handlers.cache.DirectBufferCache)1 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)1