当前位置: 首页>>代码示例>>Java>>正文


Java HttpServer.websocketHandler方法代码示例

本文整理汇总了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);
}
 
开发者ID:nielsbaloe,项目名称:vertxui,代码行数:33,代码来源:ExampleChatWebsocket.java

示例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);
    });
}
 
开发者ID:amoAHCP,项目名称:vert.x-microservice,代码行数:28,代码来源:ServiceVerticle.java

示例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);
		}
	});
}
 
开发者ID:nielsbaloe,项目名称:vertxui,代码行数:59,代码来源:FigWheelyServer.java

示例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());
        }
    });
}
 
开发者ID:SimpleQueryProtocol,项目名称:sqp,代码行数:50,代码来源:ServerVerticle.java


注:本文中的io.vertx.core.http.HttpServer.websocketHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。