本文整理汇总了Java中io.vertx.core.http.HttpServer.websocketHandler方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServer.websocketHandler方法的具体用法?Java HttpServer.websocketHandler怎么用?Java HttpServer.websocketHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.core.http.HttpServer
的用法示例。
在下文中一共展示了HttpServer.websocketHandler方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start() {
// Initialize the router and a webserver with HTTP-compression
Router router = Router.router(vertx);
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setCompressionSupported(true));
// Chat with websocket
List<String> ids = new ArrayList<>();
server.websocketHandler(socket -> {
if (!socket.path().equals("/chatWebsocket")) {
socket.reject();
return;
}
final String id = socket.textHandlerID();
ids.add(id); // entering
socket.closeHandler(data -> {
ids.remove(id); // leaving
});
socket.handler(buffer -> { // receiving
// extra: pojo example
if (Pojofy.socket(socket, Client.urlPojo, buffer, Dto.class, this::serviceDoSomething)) {
return;
}
String message = buffer.toString();
ids.forEach(i -> vertx.eventBus().send(i, message)); // broadcasting
// to reply to one: socket.writeFinalTextFrame(...);
});
});
AllExamplesServer.start(Client.class, router, server);
}
示例2: registerWebSocketHandler
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
private void registerWebSocketHandler(HttpServer server) {
server.websocketHandler((serverSocket) -> {
if (serverSocket.path().equals("wsServiceInfo")) {
// TODO implement serviceInfo request
return;
}
logDebug("connect socket to path: " + serverSocket.path());
serverSocket.pause();
serverSocket.exceptionHandler(ex -> {
//TODO
ex.printStackTrace();
});
serverSocket.drainHandler(drain -> {
//TODO
log("drain");
});
serverSocket.endHandler(end -> {
//TODO
log("end");
});
serverSocket.closeHandler(close -> {
wsHandler.findRouteSocketInRegistryAndRemove(serverSocket);
log("close");
});
wsHandler.findRouteToWSServiceAndRegister(serverSocket);
});
}
示例3: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start() {
HttpServer server = vertx.createHttpServer();
server.websocketHandler(webSocket -> {
if (!webSocket.path().equals("/" + urlSocket)) {
webSocket.reject();
return;
}
final String id = webSocket.textHandlerID();
// log.info("welcoming " + id);
vertx.sharedData().getLocalMap(browserIds).put(id, "whatever");
webSocket.closeHandler(data -> {
vertx.sharedData().getLocalMap(browserIds).remove(id);
});
webSocket.handler(buffer -> {
log.info(buffer.toString());
});
});
server.listen(portSocket, listenHandler -> {
if (listenHandler.failed()) {
log.log(Level.SEVERE, "Startup error", listenHandler.cause());
System.exit(0); // stop on startup error
}
});
// Ensure that even outside this class people can push messages to the
// browser
vertx.eventBus().consumer(figNotify, message -> {
for (Object obj : vertx.sharedData().getLocalMap(browserIds).keySet()) {
vertx.eventBus().send((String) obj, message.body());
}
});
vertx.setPeriodic(250, id -> {
if (VertxUI.isCompiling()) {
return;
}
for (Watchable watchable : watchables) {
String currentState = watchable.getCurrentState();
if (watchable.lastState.equals(currentState)) {
continue;
}
log.info("Changed: " + watchable.root);
watchable.lastState = currentState;
if (watchable.handler == null) {
log.info("Skipping recompile, no handler found.");
} else {
// Recompile
boolean succeeded = watchable.handler.translate();
if (succeeded == false) {
vertx.eventBus().publish(figNotify,
"unsuccessfull rebuild for url=" + watchable.url + " root=" + watchable.root);
}
}
vertx.eventBus().publish(figNotify, "reload: " + watchable.url);
}
});
}
示例4: start
import io.vertx.core.http.HttpServer; //导入方法依赖的package包/类
@Override
public void start() {
// TODO: set subprotocols
JsonObject config = config();
String path = config.getString("path", DEFAULT_PATH);
int port = config.getInteger("port", DEFAULT_PORT);
int poolSize = config.getInteger("connectionPoolSize", DEFAULT_POOL_SIZE);
JsonArray backendConfs = config.getJsonArray("backends");
_executorService = Executors.newFixedThreadPool(10); // TODO: set this reasonably
// Initialize the backend connection pool
BackendConnectionPool connectionPool = new VertxBackendConnectionPool(vertx, poolSize, backendConfs);
try {
connectionPool.init();
} catch (ServerErrorException e) {
_logger.log(Level.SEVERE, "Failed to create the connection pool", e);
throw new RuntimeException(e.getMessage(), e.getCause());
}
HttpServerOptions options = new HttpServerOptions();
int maxFrameSize = options.getMaxWebsocketFrameSize();
// Create the actual server
HttpServer server = vertx.createHttpServer(options);
// For each incoming websocket connection: create a client connection object
server.websocketHandler(socket -> {
if (!socket.path().equals(path)) {
socket.reject();
return;
}
// TODO: check sub protocols
new VertxClientConnection(_executorService, socket, connectionPool, maxFrameSize);
});
// start to listen
server.listen(port, result -> {
if (result.succeeded()) {
_logger.log(Level.INFO, "Listening on port " + port + " and path '" + path + "'...");
setStarted(true);
} else {
_logger.log(Level.SEVERE, "Failed to listen", result.cause());
setStartingError(result.cause());
}
});
}