use of io.vertx.core.net.NetClient in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testSendsFromOtherSideOfBridge.
@Test
public void testSendsFromOtherSideOfBridge(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("hi", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("register", "ping", null, socket);
}));
}
use of io.vertx.core.net.NetClient in project vertx-examples by vert-x3.
the class Proxy method start.
@Override
public void start() throws Exception {
NetClient client = vertx.createNetClient(new NetClientOptions());
vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.CONNECT) {
// Determine proxied server address
String proxyAddress = req.uri();
int idx = proxyAddress.indexOf(':');
String host = proxyAddress.substring(0, idx);
int port = Integer.parseInt(proxyAddress.substring(idx + 1));
System.out.println("Connecting to proxy " + proxyAddress);
client.connect(port, host, ar -> {
if (ar.succeeded()) {
System.out.println("Connected to proxy");
NetSocket clientSocket = req.netSocket();
clientSocket.write("HTTP/1.0 200 Connection established\n\n");
NetSocket serverSocket = ar.result();
serverSocket.handler(buff -> {
System.out.println("Forwarding server packet to the client");
clientSocket.write(buff);
});
serverSocket.closeHandler(v -> {
System.out.println("Server socket closed");
clientSocket.close();
});
clientSocket.handler(buff -> {
System.out.println("Forwarding client packet to the server");
serverSocket.write(buff);
});
clientSocket.closeHandler(v -> {
System.out.println("Client socket closed");
serverSocket.close();
});
} else {
System.out.println("Fail proxy connection");
req.response().setStatusCode(403).end();
}
});
} else {
req.response().setStatusCode(405).end();
}
}).listen(8080);
}
Aggregations