public static void main(String... args) throws Exception {

    Observable<WebSocketConnection> wsConnection =
        HttpClient.newClient("localhost", 8888)
            // .enableWireLogging(LogLevel.ERROR)
            .createGet("/rs")
            .requestWebSocketUpgrade()
            .flatMap(WebSocketResponse::getWebSocketConnection);

    Publisher<WebSocketDuplexConnection> connectionPublisher =
        WebSocketDuplexConnection.create(RxReactiveStreams.toPublisher(wsConnection));

    ReactiveSocket reactiveSocket =
        RxReactiveStreams.toObservable(connectionPublisher)
            .map(
                w ->
                    ReactiveSocket.fromClientConnection(
                        w, ConnectionSetupPayload.create("UTF-8", "UTF-8")))
            .toBlocking()
            .single();

    reactiveSocket.startAndWait();

    int i = ThreadLocalRandom.current().nextInt(5, 25);
    System.out.println("asking for " + i + " ints");
    ByteBuffer b = ByteBuffer.allocate(BitUtil.SIZE_OF_INT);
    b.putInt(i);

    Payload p =
        new Payload() {
          @Override
          public ByteBuffer getData() {
            b.rewind();
            return b;
          }

          @Override
          public ByteBuffer getMetadata() {
            return Frame.NULL_BYTEBUFFER;
          }
        };

    Observable<Payload> payloadObservable =
        RxReactiveStreams.toObservable(reactiveSocket.requestStream(p));
    payloadObservable
        .map(response -> response.getData().getInt(0))
        .doOnNext(r -> System.out.println("Got from server => " + r))
        .doOnError(Throwable::printStackTrace)
        .toBlocking()
        .last();
  }
Esempio n. 2
0
 public static Publisher<ByteBuffer> createPublisher(final byte[] bytes, final int chunkSize) {
   Observable<ByteBuffer> observable = Observable.from(new ByteBufferIterable(bytes, chunkSize));
   return RxReactiveStreams.toPublisher(observable);
 }
 @Override
 public Observable<Void> handle(WebSocketConnection connection) {
   Mono<Void> result = this.handler.handle(createSession(connection));
   return RxReactiveStreams.toObservable(result);
 }