@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable()); }). // Reduce all buffers in a single buffer reduce(Buffer::appendBuffer). // Turn in to a string map(buffer -> buffer.toString("UTF-8")). // Get a single buffer subscribe(data -> System.out.println("Server content " + data)); // End request req.end(); } }
/** * Create a new buffer from a string. The string will be UTF-8 encoded into the buffer. * @param string the string * @return the buffer */ public static io.vertx.rxjava.core.buffer.Buffer buffer(String string) { io.vertx.rxjava.core.buffer.Buffer ret = io.vertx.rxjava.core.buffer.Buffer.newInstance(io.vertx.core.buffer.Buffer.buffer(string)); return ret; }
@Override public io.vertx.core.buffer.Buffer unwrap(Buffer buffer) { return buffer.getDelegate(); } };
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); // Create two requests HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/"); HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/"); // Turn the requests responses into Observable<JsonObject> Observable<JsonObject> obs1 = req1.toObservable().flatMap(HttpClientResponse::toObservable). map(buf -> new JsonObject(buf.toString("UTF-8"))); Observable<JsonObject> obs2 = req2.toObservable().flatMap(HttpClientResponse::toObservable). map(buf -> new JsonObject(buf.toString("UTF-8"))); // Combine the responses with the zip into a single response obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)). subscribe(json -> { System.out.println("Got combined result " + json); }, err -> { err.printStackTrace(); }); req1.end(); req2.end(); } }
@Test public void testClusterSerializable() throws Exception { io.vertx.rxjava.core.buffer.Buffer buff = io.vertx.rxjava.core.buffer.Buffer.buffer("hello-world"); Buffer actual = Buffer.buffer(); buff.writeToBuffer(actual); Buffer expected = Buffer.buffer(); Buffer.buffer("hello-world").writeToBuffer(expected); assertEquals(expected, actual); buff = io.vertx.rxjava.core.buffer.Buffer.buffer("hello-world"); assertEquals(expected.length(), buff.readFromBuffer(0, expected)); assertEquals("hello-world", buff.toString()); }
out.write(data.getDelegate()); progress.setCurrent(bytesWritten.getAndAdd(data.length())); if (out.writeQueueFull()) { file.pause();
@Override protected Buffer buffer(String s) { return Buffer.buffer(s); }
read[0] += buf.length(); if (lastOutput[0] == 0 || read[0] - lastOutput[0] > 1024 * 2048) { logProgress(length, read[0]);
public static Buffer newInstance(io.vertx.core.buffer.Buffer arg) { return arg != null ? new Buffer(arg) : null; } }
/** * Create a new, empty buffer. * @return the buffer */ public static io.vertx.rxjava.core.buffer.Buffer buffer() { io.vertx.rxjava.core.buffer.Buffer ret = io.vertx.rxjava.core.buffer.Buffer.newInstance(io.vertx.core.buffer.Buffer.buffer()); return ret; }
/** * Same as {@link io.vertx.rxjava.core.http.HttpServerResponse#end} but writes some data to the response body before ending. If the response is not chunked and * no other data has been written then the @code{Content-Length} header will be automatically set. * @param chunk the buffer to write before ending the response */ public void end(io.vertx.rxjava.core.buffer.Buffer chunk) { delegate.end(chunk.getDelegate()); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return resp.toObservable(); }). subscribe(data -> System.out.println("Server content " + data.toString("UTF-8"))); // End request req.end(); } }
onListen.subscribe( server -> vertx.createHttpClient(new HttpClientOptions()).websocket(8080, "localhost", "/some/path", ws -> { ws.write(Buffer.buffer("foo")); ws.close(); }),
public static Buffer newInstance(io.vertx.core.buffer.Buffer arg) { return arg != null ? new Buffer(arg) : null; } }
/** * Create a new buffer given the initial size hint. * <p> * If you know the buffer will require a certain size, providing the hint can prevent unnecessary re-allocations * as the buffer is written to and resized. * @param initialSizeHint the hint, in bytes * @return the buffer */ public static io.vertx.rxjava.core.buffer.Buffer buffer(int initialSizeHint) { io.vertx.rxjava.core.buffer.Buffer ret = io.vertx.rxjava.core.buffer.Buffer.newInstance(io.vertx.core.buffer.Buffer.buffer(initialSizeHint)); return ret; }
/** * Writes a (potentially large) piece of binary data to the connection. This data might be written as multiple frames * if it exceeds the maximum WebSocket frame size. * @param data the data to write * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.core.http.WebSocketBase writeBinaryMessage(io.vertx.rxjava.core.buffer.Buffer data) { delegate.writeBinaryMessage(data.getDelegate()); return this; }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); client.put(8080, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.handler(buf -> System.out.println(buf.toString("UTF-8"))); }).setChunked(true).putHeader("Content-Type", "text/plain").write("hello").end(); } }