本文整理汇总了Java中io.vertx.core.http.ServerWebSocket类的典型用法代码示例。如果您正苦于以下问题:Java ServerWebSocket类的具体用法?Java ServerWebSocket怎么用?Java ServerWebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServerWebSocket类属于io.vertx.core.http包,在下文中一共展示了ServerWebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initWebSocketHandler
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
private Handler<ServerWebSocket> initWebSocketHandler(Router vaadinRouter, SessionStore sessionStore) {
/*
VaadinVerticleConfiguration vaadinVerticleConfiguration = getClass().getAnnotation(VaadinVerticleConfiguration.class);
String mountPoint = Optional.ofNullable(vaadinVerticleConfiguration)
.map(VaadinVerticleConfiguration::mountPoint)
.orElse(config().getString("mountPoint", "/"));
*/
String mountPoint = config().getString("mountPoint", "/");
String sessionCookieName = sessionCookieName();
WebsocketSessionHandler.WebsocketSessionHandlerBuilder websocketSessionHandlerBuilder =
WebsocketSessionHandler.builder().mountPoint(mountPoint)
.cookieName(sessionCookieName).sessionStore(sessionStore);
AtmosphereCoordinator atmosphereCoordinator = initAtmosphere(vaadinRouter, service);
router.get("/PUSH").handler(atmosphereCoordinator::route);
router.post("/PUSH").handler(atmosphereCoordinator::route);
return websocketSessionHandlerBuilder.next(atmosphereCoordinator::route).build();
}
示例2: findRouteSocketInRegistryAndRemove
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public void findRouteSocketInRegistryAndRemove(ServerWebSocket serverSocket) {
final SharedData sharedData = this.vertx.sharedData();
final String binaryHandlerID = serverSocket.binaryHandlerID();
final String textHandlerID = serverSocket.textHandlerID();
final LocalMap<String, byte[]> wsRegistry = sharedData.getLocalMap(WS_REGISTRY);
final WSEndpointHolder holder = getWSEndpointHolderFromSharedData(wsRegistry);
if (holder != null) {
final List<WSEndpoint> all = holder.getAll();
final Optional<WSEndpoint> first = all.parallelStream().filter(e -> e.getBinaryHandlerId().equals(binaryHandlerID) && e.getTextHandlerId().equals(textHandlerID)).findFirst();
first.ifPresent(endpoint -> {
holder.remove(endpoint);
wsRegistry.replace(WS_ENDPOINT_HOLDER, serialize(holder));
log("OK REMOVE: " + serverSocket.binaryHandlerID());
});
}
}
示例3: 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!!
}
示例4: start
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public void start() throws Exception {
//TODO: Fix a better way of configuration other than system properties?
Integer port = Integer.getInteger("websocket.port", 5556);
ObservableFuture<HttpServer> httpServerObservable = RxHelper.observableFuture();
HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(port));
httpServerObservable.subscribe(
a -> log.info("Starting web socket listener..."),
e -> log.error("Could not start web socket listener at port " + port, e),
() -> log.info("Started web socket listener on port " + port)
);
Observable<Tup2<ServerWebSocket, Func1<Event, Boolean>>> eventObservable = EventObservable.convertFromWebSocketObservable(RxHelper.toObservable(httpServer.websocketStream()));
eventObservable.subscribe(new EventToJsonAction(Riemann.getEvents(vertx), WebSocketFrameImpl::new), e -> {
log.error(e);
//TODO: Fix proper error handling
});
httpServer.listen(httpServerObservable.asHandler());
}
示例5: 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());
}
示例6: 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));
}
示例7: upgrade
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public StopWatch upgrade(StopWatch requestWatch, ServerWebSocket serverWebSocket) {
requestWatch.stop();
counterService.increment("requests.upgraded");
requestWatch.start("websocket");
return requestWatch;
}
示例8: connected
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public StopWatch connected(StopWatch socketMetric, ServerWebSocket serverWebSocket) {
counterService.increment("websockets.connected");
StopWatch websocketWatch = new StopWatch();
websocketWatch.start("websocket");
return websocketWatch;
}
示例9: testWebsocketHandler
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Test
public void testWebsocketHandler(TestContext context) {
Handler<ServerWebSocket> handler = new Handler<ServerWebSocket>() {
@Override
public void handle(ServerWebSocket event) {
}
};
context.assertEquals(server, server.websocketHandler(handler));
context.assertEquals(handler, server.websocketHandler());
}
示例10: handle
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public void handle(ServerWebSocket serverWebSocket) {
String basePath = Optional.ofNullable(mountPoint)
.map(m -> m.substring(0, m.lastIndexOf('/')) )
.orElse("");
if (!serverWebSocket.path().startsWith(basePath + "/PUSH")) {
serverWebSocket.reject();
}
String cookieHeader = serverWebSocket.headers().get(COOKIE);
if (cookieHeader != null) {
Optional<String> sessionId = ServerCookieDecoder.STRICT.decode(cookieHeader).stream()
.filter(cookie -> cookieName.equals(cookie.name()))
.findFirst().map(Cookie::value);
if (sessionId.isPresent()) {
sessionId.ifPresent(sid -> sessionStore.get(sid, event -> {
Session session = null;
if (event.succeeded()) {
session = event.result();
}
next.accept(serverWebSocket, session);
}
));
return;
}
}
next.accept(serverWebSocket, null);
}
示例11: start
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public void start(Future<Void> fut) {
vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {
public void handle(final ServerWebSocket ws) {
final String id = ws.textHandlerID();
System.out.println("new connection from"+ ws.toString() + "id "+id);
vertx.eventBus().consumer("chat",message -> {
ws.writeFinalTextFrame((String) message.body());
});
ws.handler(new Handler<Buffer>() {
public void handle(Buffer data) {
// When our websocket receive data we publish it to our consumer
vertx.eventBus().publish("chat",data.toString());
}
});
ws.closeHandler(handler ->{
System.out.println("Close WS ");
});
}}
).requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
req.response().end("Chat");
//Not usefull but it display chat on our browser
}
}).listen(8080);
}
示例12: connected
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public Void connected(Void socketMetric, ServerWebSocket serverWebSocket) {
long value = wsConnections.incrementAndGet();
setMetric(SENSISION_CLASS_WEBSOCKETS, defaultLabels, value);
incrementMetric(SENSISION_CLASS_WEBSOCKET_CONNECTED_COUNT, defaultLabels);
return null;
}
示例13: WebsocketWriteStream
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
public WebsocketWriteStream(ServerWebSocket socket, int maxFrameSize) {
_socket = socket;
_buffer = Buffer.buffer(maxFrameSize + 1024);
_maxFrameSize = maxFrameSize;
_format = DataFormat.Text;
_waitForDrain = false;
}
示例14: VertxClientConnection
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
public VertxClientConnection(ExecutorService executorService, ServerWebSocket websocket, BackendConnectionPool connectionPool, int maxFrameSize) {
_executorService = executorService;
_maxFrameSize = maxFrameSize;
_messageFormat = DataFormat.Text;
_socket = websocket;
_session = new ClientSession(connectionPool, this);
_msgReceiver = new MessageReceiver(_session, new JacksonMessageDecoder());
logger = Logger.getGlobal();
initSendingPump();
registerHandlers();
logger.log(Level.INFO, "New client connected via websocket.");
}
示例15: webSocketHandler
import io.vertx.core.http.ServerWebSocket; //导入依赖的package包/类
@Override
public Handler<ServerWebSocket> webSocketHandler() {
if (!options.isWebsocketBridge()) {
return null;
}
StompServerHandler stomp;
synchronized (this) {
stomp = this.handler;
}
return socket -> {
if (!socket.path().equals(options.getWebsocketPath())) {
LOGGER.error("Receiving a web socket connection on an invalid path (" + socket.path() + "), the path is " +
"configured to " + options.getWebsocketPath() + ". Rejecting connection");
socket.reject();
return;
}
StompServerConnection connection = new StompServerWebSocketConnectionImpl(socket, this, writingFrameHandler);
FrameParser parser = new FrameParser(options);
socket.exceptionHandler((exception) -> {
LOGGER.error("The STOMP server caught a WebSocket error - closing connection", exception);
connection.close();
});
socket.endHandler(v -> connection.close());
parser
.errorHandler((exception) -> {
connection.write(
Frames.createInvalidFrameErrorFrame(exception));
connection.close();
}
)
.handler(frame -> stomp.handle(new ServerFrameImpl(frame, connection)));
socket.handler(parser);
};
}