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


Java ServerWebSocket.handler方法代码示例

本文整理汇总了Java中io.vertx.core.http.ServerWebSocket.handler方法的典型用法代码示例。如果您正苦于以下问题:Java ServerWebSocket.handler方法的具体用法?Java ServerWebSocket.handler怎么用?Java ServerWebSocket.handler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.vertx.core.http.ServerWebSocket的用法示例。


在下文中一共展示了ServerWebSocket.handler方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendToWSService

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
private void sendToWSService(final ServerWebSocket serverSocket, final String path, final WSEndpoint endpoint) {
    final EventBus eventBus = vertx.eventBus();
    serverSocket.handler(handler -> {
                try {
                    log("send WS:+ " + endpoint.getUrl());
                    eventBus.send(path, Serializer.serialize(new WSDataWrapper(endpoint, handler.getBytes())), new DeliveryOptions().setSendTimeout(GlobalKeyHolder.DEFAULT_SERVICE_TIMEOUT));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    );
    serverSocket.resume();
    //TODO set close handler!!
}
 
开发者ID:amoAHCP,项目名称:vert.x-microservice,代码行数:17,代码来源:WSLocalHandler.java

示例2: handle

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
/**
 * Handles a web socket connection.
 *
 * @param socket the opening socket.
 */
@Override
public void handle(final ServerWebSocket socket) {
    LOGGER.info("New web socket connection {}, {}", socket, socket.uri());

    if (! configuration.accept(socket.uri())) {
        LOGGER.warn("Web Socket connection denied on {} by {}", socket.uri(), configuration.name());
        return;
    }

    final Socket sock = new Socket(socket);
    accessor.getDispatcher().addSocket(socket.path(), sock);

    socket.closeHandler(event -> {
        LOGGER.info("Web Socket closed {}, {}", socket, socket.uri());
        accessor.getDispatcher().removeSocket(socket.path(), sock);
    });

    socket.handler(event -> accessor.getDispatcher().received(socket.path(), event.getBytes(), sock));

}
 
开发者ID:wisdom-framework,项目名称:wisdom,代码行数:26,代码来源:WebSocketHandler.java

示例3: sendToWSService

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
private void sendToWSService(final ServerWebSocket serverSocket, final EventBus eventBus, final String path, final WSEndpoint endpoint) {
    serverSocket.handler(handler -> {
                try {
                    eventBus.send(path, Serializer.serialize(new WSDataWrapper(endpoint, handler.getBytes())), new DeliveryOptions().setSendTimeout(GlobalKeyHolder.DEFAULT_SERVICE_TIMEOUT));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    );
    serverSocket.resume();
    //TODO set close handler!!
}
 
开发者ID:amoAHCP,项目名称:vert.x-microservice,代码行数:15,代码来源:WSClusterHandler.java

示例4: accept

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
public void accept(ServerWebSocket ws){
    final String path = ws.path();
    if(routes.containsKey(path)) {
           sessions.put(ws,path);
           Handler handler = routes.get(path);
           ws.handler(handler);
    }else {
        //TODO error handling
    }
}
 
开发者ID:amoAHCP,项目名称:vert.x-microservice,代码行数:11,代码来源:WSRouteMatcher.java

示例5: route

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
public AtmosphereCoordinator route(ServerWebSocket webSocket, Session session) {
    Map<String, List<String>> paramMap = new QueryStringDecoder("?" + webSocket.query()).parameters();
    Map<String, String[]> params = new LinkedHashMap<String, String[]>(paramMap.size());
    for (Map.Entry<String, List<String>> entry : paramMap.entrySet()) {
        params.put(entry.getKey(), entry.getValue().toArray(new String[]{}));
    }

    String contentType = "application/json";
    if (params.size() == 0) {
        // TODO: vert.x trim the query string, unfortunately.
        params.put(X_ATMO_PROTOCOL, new String[]{"true"});
        params.put(X_ATMOSPHERE_FRAMEWORK, new String[]{"2.1"});
        params.put(X_ATMOSPHERE_TRACKING_ID, new String[]{"0"});
        params.put(X_ATMOSPHERE_TRANSPORT, new String[]{"websocket"});
        params.put("Content-Type", new String[]{contentType});
    } else if (params.containsKey("Content-Type") && params.get("Content-Type").length > 0) {
        contentType = params.get("Content-Type")[0];
    }

    AtmosphereRequest.Builder requestBuilder = new AtmosphereRequest.Builder()
        .requestURI(webSocket.path())
        .requestURL("http://0.0.0.0" + webSocket.path())
        .contentType(contentType)
        .pathInfo(webSocket.path())
        .queryStrings(params);

    Optional.ofNullable(session)
        .map(ExtendedSession::adapt)
        .map(VertxHttpSession::new)
        .ifPresent(requestBuilder::session);

    AtmosphereRequest r = requestBuilder.build();

    HttpServerRequest request = requestfromAtmosphere(r);
    RoutingContext routingContext = new RoutingContextImpl(null, null, request, Collections.emptySet());
    routingContext.setSession(session);
    r.setAttribute(RoutingContext.class.getName(), routingContext);
    r.setAttribute(HttpServerRequest.class.getName(), request);


    final WebSocket w = new VertxWebSocket(framework.getAtmosphereConfig(), webSocket);
    try {
        webSocketProcessor.open(w, r, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), r, w));
    } catch (IOException e) {
        logger.debug("", e);
    }

    webSocket.handler(new Handler<Buffer>() {
        @Override
        public void handle(Buffer data) {
            webSocketProcessor.invokeWebSocketProtocol(w, data.toString());
        }
    });
    webSocket.exceptionHandler(new Handler<Throwable>() {
        @Override
        public void handle(Throwable event) {
            w.close();
            logger.debug("", event);
            webSocketProcessor.close(w, 1006);
        }
    });
    webSocket.closeHandler(new VoidHandler() {
        @Override
        protected void handle() {
            w.close();
            webSocketProcessor.close(w, 1005);
        }
    });
    return this;
}
 
开发者ID:mcollovati,项目名称:vaadin-vertx-samples,代码行数:71,代码来源:AtmosphereCoordinator.java

示例6: handle

import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
@Override
public void handle(final ServerWebSocket ws) {
    ws.setWriteQueueMaxSize(Constants.QUEUEMAXSIZE);
    final MultiMap params = params(ws.uri());
    if (ws.path().equals("/connect") && params.contains("sid") && params.contains("ack") && params.contains("pos")) {
        final UUID sid = UUID.fromString(params.get("sid"));
        final LocalMap<String, Session> map = vertx.sharedData().getLocalMap(Constants.SESSIONS);
        final Session session = map.get(sid.toString());
        if (session == null || !session.isActive()) {
            ws.reject();
            return;
        }
        session.setRead_count(Integer.parseInt(params.get("ack")));
        session.setWrite_count(Integer.parseInt(params.get("pos")));
        final TransferObserver observer = new TransferObserver(session, ws);
        final TransferQueue queue;
        try {
            queue = QueueFactory.getQueue(sid.toString());
        } catch (final NoSuchQueueException ex) {
            logger.warn(ex, ex.fillInStackTrace());
            ws.reject();
            ws.close();
            return;
        }
        if (queue.countObservers() == 0) {
            queue.addObserver(observer);
        }
        final Buffer buffer = queue.peek();
        if (buffer != null) {
            if (!ws.writeQueueFull()) {
                final Buffer ackbuffer = Buffer.buffer();
                ackbuffer.setInt(0, session.getWrite_count());
                ackbuffer.setBuffer(4, buffer);
                ws.write(ackbuffer);
                queue.remove(buffer);
            } else {
                ws.pause();
            }
        }
        logger.debug("connected");
        ws.drainHandler(v -> ws.resume());
        ws.handler(data -> {
            if (!session.isActive()) {
                ws.close();
                return;
            }
            if (data.length() < 4) {
                logger.warn("wrong frame format");
                return;
            }
            session.setWrite_count(session.getWrite_count() + data.length() - 4);
            vertx.eventBus().publish(session.getHandler(), data.getBuffer(4, data.length()));
        });
        ws.closeHandler(v -> {
            queue.deleteObservers();
            logger.debug("disconnected");
        });
    } else {
        ws.reject();
    }
}
 
开发者ID:zyclonite,项目名称:nassh-relay,代码行数:62,代码来源:ConnectHandler.java


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