public interface WebServer extends Endpoint<WebServer>
Configures an event based webserver.
To create an instance, use WebServers.createWebServer(int).
As with many of the interfaces in cauli, setter style methods return a reference to this, to allow for simple initialization using method chaining.
class HelloWorldHandler implements HttpHandler {
void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) {
response.header("Content-Type", "text/html")
.content("Hello World")
.end();
}
}
WebServer webServer = WebServers.createWebServer(8080)
.add(new HelloWorldHandler());
webServer.start();
print("Point your browser to " + webServer.getUri());
WebServer webServer = WebServers.createWebServer(8080)
.add(new StaticFileHandler("./wwwdata"));
webServer.start();
WebServers,
HttpHandler,
WebSocketConnection,
EventSourceConnection| 限定符和类型 | 方法和说明 |
|---|---|
WebServer |
add(HttpHandler handler)
Add an HttpHandler.
|
WebServer |
add(String path,
EventSourceHandler handler)
Add a WebSocketHandler for dealing with WebSockets.
|
WebServer |
add(String path,
HttpHandler handler)
Add an HttpHandler that will only respond to a certain path (e.g "/some/page").
|
WebServer |
add(String path,
WebSocketHandler handler)
Add a WebSocketHandler for dealing with WebSockets.
|
int |
getPort()
Get base port that webserver is serving on.
|
WebServer |
setupSsl(InputStream keyStore,
String storePass,
String keyPass)
Setup SSL/TLS handler
|
WebServer |
staleConnectionTimeout(long millis)
Number of milliseconds before a stale HTTP keep-alive connection is closed by the server.
|
connectionExceptionHandler, getExecutor, getUri, setupSsl, start, stop, uncaughtExceptionHandlerWebServer add(HttpHandler handler)
HttpControl.nextHandler()). This is repeated
until a HttpHandler returns a response. If there are no remaining handlers, the
webserver shall return 404 NOT FOUND to the browser.
HttpHandlers are attempted in the order in which they are added to the WebServer.HttpHandlerWebServer add(String path, HttpHandler handler)
add(newPathMatchHandler(path, handler)).WebServer add(String path, WebSocketHandler handler)
add(new PathMatchHandler(path, newHttpToWebSocketHandler(handler))).WebServer add(String path, EventSourceHandler handler)
add(new PathMatchHandler(path, newHttpToEventSourceHandler(handler))).int getPort()
WebServer staleConnectionTimeout(long millis)
WebServer setupSsl(InputStream keyStore, String storePass, String keyPass) throws WebbitException
keyStore - Keystore InputStreamstorePass - Store passwordkeyPass - Key passwordWebbitException - A problem loading the keystoreCopyright © 2014. All rights reserved.