Last active
March 17, 2018 19:34
-
-
Save shvalb/a50be68180e2b1db7012f2a0c463f8d2 to your computer and use it in GitHub Desktop.
Vertx NetClient used with JsonParser - Only returns the 1st message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.luckyrhinos.sbg.robots; | |
| import static io.vertx.core.parsetools.JsonEventType.VALUE; | |
| import static org.awaitility.Awaitility.await; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.concurrent.atomic.AtomicBoolean; | |
| import org.junit.Test; | |
| import com.luckyrhinos.core.common.utils.RefHolder; | |
| import io.vertx.reactivex.core.Vertx; | |
| import io.vertx.reactivex.core.net.NetClient; | |
| import io.vertx.reactivex.core.net.NetSocket; | |
| import io.vertx.reactivex.core.parsetools.JsonParser; | |
| import io.vertx.reactivex.core.streams.Pump; | |
| public class JsonRequestTest { | |
| @Test | |
| public void testName() throws Exception { | |
| Vertx vertx = Vertx.vertx(); | |
| NetClient client = vertx.createNetClient(); | |
| RefHolder<NetSocket> hSocket = new RefHolder<>(); | |
| AtomicBoolean ready = new AtomicBoolean(false); | |
| client.rxConnect(12344, "localhost") // | |
| .subscribe(socket -> { | |
| Pump.pump(socket, socket).start(); | |
| JsonParser parser = JsonParser.newParser().objectValueMode(); | |
| hSocket.obj = socket; | |
| socket // | |
| .exceptionHandler(e -> { | |
| e.printStackTrace(); | |
| client.close(); | |
| }) // | |
| .endHandler(v -> { | |
| System.out.println("Done!"); | |
| client.close(); | |
| }) // | |
| .handler(parser::handle); | |
| parser.handler(event -> { | |
| if (event.type() == VALUE) { | |
| System.out.println(event.value()); | |
| } | |
| }); | |
| ready.set(true); | |
| }); | |
| await().atMost(5, TimeUnit.SECONDS).untilTrue(ready); | |
| ready.set(false); | |
| hSocket.obj.write("{ \"type\":\"SetupRequest\",\"ply\":2 }"); | |
| hSocket.obj.write("{ \"type\":\"PingRequest\", \"id\":\"123456789\" }"); | |
| await().atMost(5, TimeUnit.SECONDS).untilTrue(ready); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment