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();
}
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();
}
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();
}
Aggregations