本文整理汇总了Java中org.vertx.java.core.http.HttpServer类的典型用法代码示例。如果您正苦于以下问题:Java HttpServer类的具体用法?Java HttpServer怎么用?Java HttpServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpServer类属于org.vertx.java.core.http包,在下文中一共展示了HttpServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startLocalHttpServer
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
private void startLocalHttpServer(final URI address) {
String host = address.getHost();
int port = address.getPort();
m_logger.info("Listening on {}:{}", host, port);
HttpServer server = vertx.createHttpServer();
server.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
m_logger.debug("{} received request from {}",
request.localAddress(),
request.remoteAddress());
request.bodyHandler(new Handler<Buffer>() {
@Override
public void handle(final Buffer body) {
m_logger.info("{} client data: {}",
request.localAddress(),
body.toString(StandardCharsets.UTF_8.name()));
}
});
request.response().setStatusCode(200).end();
}
}).listen(port, host);
}
示例2: EmulatorLaunchpad
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
/**
* Constructor.
*
* @param httpPort The HTTP port on which the emulator should run.
*/
public EmulatorLaunchpad(int httpPort) {
vertx = VertxFactory.newVertx();
// Static files
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(new WebResourceHandler());
// Eventbus bridge
JsonObject bridgeConfig = new JsonObject().putString("prefix", EVENTBUS_ADDRESS);
JsonArray credentialsPermitAll = new JsonArray().add(new JsonObject());
vertx.createSockJSServer(httpServer).bridge(bridgeConfig, credentialsPermitAll, credentialsPermitAll);
vertx.eventBus().registerLocalHandler(EVENTBUS_SERVER_HANDLER_ID, eventBusHandler);
System.out.println("Launchpad emulator is ready on http://localhost:" + httpPort + "/");
httpServer.listen(httpPort);
}
示例3: startServer
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
/**
* Start an embedded HTTP server
*
* @param activeProfilers The active profilers
* @param port The port on which to bind the server
*/
public static void startServer(final Map<String, ScheduledFuture<?>> runningProfilers, final Map<String, Profiler> activeProfilers, final int port, final AtomicReference<Boolean> isRunning, final List<String> errors) {
final HttpServer server = VERTX.createHttpServer();
server.requestHandler(RequestHandler.getMatcher(runningProfilers, activeProfilers, isRunning, errors));
server.listen(port, new Handler<AsyncResult<HttpServer>>() {
@Override
public void handle(AsyncResult<HttpServer> event) {
if (event.failed()) {
server.close();
startServer(runningProfilers, activeProfilers, port + 1, isRunning, errors);
} else if (event.succeeded()) {
LOGGER.info("Profiler server started on port " + port);
}
}
});
}
示例4: startSockJSServer
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
private void startSockJSServer() {
HttpServer httpServer = vertx.createHttpServer();
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
JsonObject config = new JsonObject().putString("prefix", "/chat");
sockJSServer.installApp(config, new Handler<SockJSSocket>() {
public void handle(final SockJSSocket sock) {
vertx.sharedData().getSet(SHARED_DATA_SOCKET_IDS).add(sock.writeHandlerID());
sock.endHandler(new Handler<Void>() {
@Override
public void handle(Void aVoid) {
vertx.sharedData().getSet(SHARED_DATA_SOCKET_IDS).remove(sock.writeHandlerID());
}
});
}
});
httpServer.listen(8083);
}
示例5: checkAndCreateHttpServer
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
private HttpServer checkAndCreateHttpServer(String host, int port) {
if (isAddressAlreadyInUse(host, port)) {
System.err.println(host + ":" + port + " isAddressAlreadyInUse!");
System.exit(1);
return null;
}
try {
this.host = host;
this.port = port;
Vertx vertx = VertxFactory.newVertx();
return vertx.createHttpServer();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例6: registerHttpHandler
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
final public void registerHttpHandler(String host, int port,
Handler<HttpServerRequest> handler) {
HttpServer server = checkAndCreateHttpServer(host, port);
if (server == null) {
return;
}
server.requestHandler(handler).listen(port, host);
System.out.println("Started server Ok at "+host+":"+port);
foreverLoop(1000);
}
示例7: createHttpServer
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
private void createHttpServer(int listenPort, final String expectedResponseMediaType, final String expectedResponse) {
HttpServer server = vertx.createHttpServer();
server.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.bodyHandler(new Handler<Buffer>() {
@Override
public void handle(Buffer body) {
String contentType = request.headers().get("Content-Type");
assertEquals(expectedResponseMediaType, contentType);
assertEquals(expectedResponse, body.toString());
testComplete();
}
});
}
});
server.listen(listenPort);
}
示例8: executeApp
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
@Override
public void executeApp() throws Exception {
int port = getAvailablePort();
HttpServer httpServer = new DefaultVertxFactory().createVertx().createHttpServer()
.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
req.response.end("Hello World!");
}
}).listen(port);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:" + port + "/abc?xyz=123");
int statusCode = httpClient.execute(httpGet).getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new IllegalStateException("Unexpected status code: " + statusCode);
}
httpServer.close();
}
示例9: executeApp
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
@Override
public void executeApp() throws Exception {
int port = getAvailablePort();
HttpServer httpServer = new DefaultVertxFactory().createVertx().createHttpServer()
.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
req.response().end("Hello World!");
}
}).listen(port);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:" + port + "/abc?xyz=123");
int statusCode = httpClient.execute(httpGet).getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new IllegalStateException("Unexpected status code: " + statusCode);
}
httpServer.close();
}
示例10: registerWebsocketHandler
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
/**
* Registers onMessage and onClose message handler for WebSockets
*
* @param httpServer
*/
private void registerWebsocketHandler(final HttpServer httpServer) {
httpServer.websocketHandler((serverSocket) -> {
repository.addWebSocket(serverSocket);
final String path = serverSocket.path();
switch (path) {
case "/all":
// reply to first contact
serverSocket.writeTextFrame("hallo1");
// add handler for further calls
serverSocket.dataHandler(data -> {
serverSocket.writeTextFrame("hallo2");
});
break;
case "/update":
serverSocket.dataHandler(data -> vertx.eventBus().publish("org.jacpfx.petstore.add", data.getBytes()));
break;
case "/updateAll":
serverSocket.dataHandler(data -> vertx.eventBus().publish("org.jacpfx.petstore.addAll", data.getBytes()));
break;
}
serverSocket.closeHandler((close) -> handleConnectionClose(close, serverSocket));
});
}
示例11: postStart
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
@Override
protected void postStart(JsonObject appConfig, HttpServer server) {
Handler<Message<JsonObject>> myHandler = new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> message) {
String action = message.body().getString("action");
String socketId = message.body().getString("socketId");
if("message".equals(action)){
message.body().putString("action", "LOGIN");
eb.send(socketId, new Buffer(message.body().encode()));
}
}
};
eb.registerHandler(MessageServer.class.getName(), myHandler, new AsyncResultHandler<Void>() {
public void handle(AsyncResult<Void> asyncResult) {
System.out.println("["+this.getClass().getName()+"] has been registered across the cluster ok? " + asyncResult.succeeded());
}
});
}
示例12: start
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
@Override
void start(JsonObject appConfig, HttpServer server) {
container.deployWorkerVerticle(SampleVerticle.class.getName(),new JsonObject(),1,false);
eb = vertx.eventBus();
logger = container.logger();
client = vertx.createHttpClient().setPort(8080).setHost("localhost");
client.setConnectTimeout(5000);
System.out.println("SERVER --------");
/*
long timerID = vertx.setPeriodic(1000, new Handler<Long>() {
public void handle(Long timerID) {
logger.info("And every second this is printed");
}
});
System.out.print(timerID);
*/
}
示例13: bridge
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
public void bridge(final CountingCompletionHandler<Void> countDownLatch) {
HttpServer server = vertx.createHttpServer();
SockJSServer sjsServer = vertx.createSockJSServer(server).setHook(hook);
JsonObject empty = new JsonObject();
JsonArray all = new JsonArray().add(empty);
JsonArray inboundPermitted = config.getArray("inbound_permitted", all);
JsonArray outboundPermitted = config.getArray("outbound_permitted", all);
sjsServer.bridge(config.getObject("sjs_config", new JsonObject()
.putString("prefix", "/channel")), inboundPermitted, outboundPermitted, config.getObject(
"bridge_config", empty));
countDownLatch.incRequired();
server.listen(config.getInteger("port", 1986), config.getString("host", "0.0.0.0"),
new AsyncResultHandler<HttpServer>() {
@Override
public void handle(AsyncResult<HttpServer> ar) {
if (!ar.succeeded()) {
countDownLatch.failed(ar.cause());
} else {
countDownLatch.complete();
}
}
});
}
示例14: start
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
public void start() {
RouteMatcher routeMatcher = new RouteMatcher();
// HTTP server
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(routeMatcher);
// SockJS server
JsonArray permitted = new JsonArray();
permitted.add(new JsonObject()); // Let everything through
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
sockJSServer.bridge(new JsonObject().putString("prefix", "/eventbus"), permitted, permitted);
httpServer.listen(7777);
System.out.println("Vert.X Core UP");
}
示例15: startRestEndpoint
import org.vertx.java.core.http.HttpServer; //导入依赖的package包/类
public HttpServer startRestEndpoint() throws InterruptedException {
restEndpointServer = vertx.createHttpServer();
restEndpointServer.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest request) {
request.response().putHeader("content-type", "text/plain");
request.response().end("Hello World!");
}
});
FutureHandler<AsyncResult<HttpServer>> future = new FutureHandler<>();
restEndpointServer.listen(8181, "0.0.0.0", future);
future.await();
return restEndpointServer;
}