io.vertx.core.http.WebSocketBase Java Examples

The following examples show how to use io.vertx.core.http.WebSocketBase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VertxWebSocketSession.java    From vertx-spring-boot with Apache License 2.0 6 votes vote down vote up
private void messageHandler(WebSocketBase socket, WebSocketMessage message) {
    if (message.getType() == WebSocketMessage.Type.TEXT) {
        String payload = message.getPayloadAsText();
        socket.writeTextMessage(payload);
    } else {
        Buffer buffer = bufferConverter.toBuffer(message.getPayload());

        if (message.getType() == WebSocketMessage.Type.PING) {
            socket.writePing(buffer);
        } else if (message.getType() == WebSocketMessage.Type.PONG) {
            socket.writePong(buffer);
        } else {
            socket.writeBinaryMessage(buffer);
        }
    }
}
 
Example #2
Source File: WebSocketServiceTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void websocketServiceRemoveSubscriptionOnConnectionClose(final TestContext context) {
  final Async async = context.async();

  vertx
      .eventBus()
      .consumer(SubscriptionManager.EVENTBUS_REMOVE_SUBSCRIPTIONS_ADDRESS)
      .handler(
          m -> {
            context.assertNotNull(m.body());
            async.complete();
          })
      .completionHandler(v -> httpClient.websocket("/", WebSocketBase::close));

  async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
}
 
Example #3
Source File: EventbusBridgeTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testHookSocketClosed() throws Exception {

  sockJSHandler.bridge(allAccessOptions, be -> {
    if (be.type() == BridgeEventType.SOCKET_CLOSED) {
      assertNotNull(be.socket());
      assertNull(be.getRawMessage());
      be.complete(true);
      testComplete();
    } else {
      be.complete(true);
    }
  });
  client.webSocket(websocketURI, onSuccess(WebSocketBase::close));
  await();
}
 
Example #4
Source File: VertxWebSocketSession.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public Flux<WebSocketMessage> receive() {
    return Flux.create(sink -> {
            logger.debug("{}Connecting to a web socket read stream", getLogPrefix());
            WebSocketBase socket = getDelegate();
            socket.pause()
                .textMessageHandler(payload -> {
                    logger.debug("{}Received text '{}' from a web socket read stream", getLogPrefix(), payload);
                    sink.next(textMessage(payload));
                })
                .binaryMessageHandler(payload -> {
                    logger.debug("{}Received binary '{}' from a web socket read stream", getLogPrefix(), payload);
                    sink.next(binaryMessage(payload));
                })
                .pongHandler(payload -> {
                    logger.debug("{}Received pong '{}' from a web socket read stream", getLogPrefix(), payload);
                    sink.next(pongMessage(payload));
                })
                .exceptionHandler(throwable -> {
                    logger.debug("{}Received exception '{}' from a web socket read stream", getLogPrefix(), throwable);
                    sink.error(throwable);
                })
                .endHandler(e -> {
                    logger.debug("{}Web socket read stream ended", getLogPrefix());
                    sink.complete();
                });
            sink.onRequest(i -> {
                logger.debug("{}Fetching '{}' entries from a web socket read stream", getLogPrefix(), i);
                socket.fetch(i);
            });
        }
    );
}
 
Example #5
Source File: VertxWebSocketSession.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> send(Publisher<WebSocketMessage> messages) {
    return Mono.create(sink -> {
        logger.debug("{}Subscribing to messages publisher", getLogPrefix());
        Subscriber<WebSocketMessage> subscriber =
            new WriteStreamSubscriber.Builder<WebSocketBase, WebSocketMessage>()
                .writeStream(getDelegate())
                .nextHandler(this::messageHandler)
                .endHook(sink)
                .build();
        messages.subscribe(subscriber);
    });
}
 
Example #6
Source File: TestEventBusBridge.java    From nubes with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseEvent(TestContext context) throws Exception {
  Async async = context.async();
  vertx.eventBus().consumer(BridgeEventType.SOCKET_CLOSED.toString(), ebMsg -> {
    context.assertEquals(BridgeEventType.SOCKET_CLOSED.toString(), ebMsg.body());
    async.complete();
  });
  client().websocket("/eventbus/default/websocket", WebSocketBase::close);
}
 
Example #7
Source File: SockJSWriteTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testWebSocketFailure() throws Exception {
  String expected = TestUtils.randomAlphaString(64);
  socketHandler = () -> socket -> {
    socket.endHandler(v -> {
      socket.write(Buffer.buffer(expected), onFailure(err -> {
        testComplete();
      }));
    });
  };
  startServers();
  client.webSocket("/test/400/8ne8e94a/websocket", onSuccess(WebSocketBase::close));
  await();
}
 
Example #8
Source File: SockJSWriteTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testRawFailure() throws Exception {
  String expected = TestUtils.randomAlphaString(64);
  socketHandler = () -> socket -> {
    socket.endHandler(v -> {
      socket.write(Buffer.buffer(expected), onFailure(err -> {
        testComplete();
      }));
    });
  };
  startServers();
  client.webSocket("/test/websocket", onSuccess(WebSocketBase::close));
  await();
}
 
Example #9
Source File: ServerWebSocketWrapper.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public WebSocketBase textMessageHandler(@Nullable Handler<String> handler) {
  delegate.textMessageHandler(handler);
  return this;
}
 
Example #10
Source File: MQTTWebSocketWrapper.java    From vertx-mqtt-broker with Apache License 2.0 4 votes vote down vote up
public MQTTWebSocketWrapper(WebSocketBase netSocket) {
    super(netSocket);
}
 
Example #11
Source File: WebSocketWrapper.java    From vertx-mqtt-broker with Apache License 2.0 4 votes vote down vote up
public WebSocketWrapper(WebSocketBase netSocket) {
    super(netSocket, netSocket);
}
 
Example #12
Source File: EventBusWebsocketBridge.java    From vertx-mqtt-broker with Apache License 2.0 4 votes vote down vote up
public EventBusWebsocketBridge(WebSocketBase webSocket, EventBus eventBus, String eventBusAddress) {
    this.eventBus = eventBus;
    this.webSocket = webSocket;
    this.eventBusAddress = eventBusAddress;
    this.bridgeUUID = UUID.randomUUID().toString();
}
 
Example #13
Source File: ServerWebSocketWrapper.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public WebSocketBase pongHandler(@Nullable Handler<Buffer> handler) {
  delegate.pongHandler(handler);
  return this;
}
 
Example #14
Source File: ServerWebSocketWrapper.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public WebSocketBase binaryMessageHandler(@Nullable Handler<Buffer> handler) {
  delegate.binaryMessageHandler(handler);
  return this;
}
 
Example #15
Source File: ServerWebSocketWrapper.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public WebSocketBase writePong(Buffer data, Handler<AsyncResult<Void>> handler) {
  delegate.writePong(data, handler);
  return this;
}
 
Example #16
Source File: ServerWebSocketWrapper.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public WebSocketBase writePing(Buffer data, Handler<AsyncResult<Void>> handler) {
  delegate.writePing(data, handler);
  return this;
}
 
Example #17
Source File: VertxWebSocketSession.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
public VertxWebSocketSession(WebSocketBase delegate, HandshakeInfo handshakeInfo, BufferConverter bufferConverter) {
    super(delegate, ObjectUtils.getIdentityHexString(delegate), handshakeInfo,
        bufferConverter.getDataBufferFactory());
    this.bufferConverter = bufferConverter;
}