本文整理汇总了Java中io.vertx.core.http.ServerWebSocket.closeHandler方法的典型用法代码示例。如果您正苦于以下问题:Java ServerWebSocket.closeHandler方法的具体用法?Java ServerWebSocket.closeHandler怎么用?Java ServerWebSocket.closeHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.core.http.ServerWebSocket
的用法示例。
在下文中一共展示了ServerWebSocket.closeHandler方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: call
import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
@Override
public void call(Tup2<ServerWebSocket, Func1<Event, Boolean>> r) {
ServerWebSocket socket = r.getLeft();
Subscription subscription = eventObservable
.filter(r.getRight())
.map(e -> new JsonObject()
.put("tags", e.getTags())
.put("host", e.getHost())
.put("state", e.getState())
.put("service", e.getService())
.put("description", e.getDescription())
.put("metric", e.getMetric())
.put("time", e.getTime())
.put("ttl", e.getTtl())
)
.subscribe(json -> socket.writeFrame(frameFactory.call(json.encode())));
socket.closeHandler(h -> subscription.unsubscribe());
}
示例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));
}
示例3: WebSocketListener
import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
WebSocketListener(ServerWebSocket ws, SockJSSession session) {
this.ws = ws;
this.session = session;
ws.textMessageHandler(this::handleMessages);
ws.closeHandler(v -> {
closed = true;
session.shutdown();
});
ws.exceptionHandler(t -> {
closed = true;
session.shutdown();
session.handleException(t);
});
}
示例4: RawWSSockJSSocket
import io.vertx.core.http.ServerWebSocket; //导入方法依赖的package包/类
RawWSSockJSSocket(Vertx vertx, Session webSession, User webUser, ServerWebSocket ws) {
super(vertx, webSession, webUser);
this.ws = ws;
ws.closeHandler(v -> {
// Make sure the writeHandler gets unregistered
RawWSSockJSSocket.super.close();
});
}
示例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;
}
示例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();
}
}