Search in sources :

Example 51 with NetClient

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);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 52 with NetClient

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);
}
Also used : HttpMethod(io.vertx.core.http.HttpMethod) AbstractVerticle(io.vertx.core.AbstractVerticle) NetClient(io.vertx.core.net.NetClient) Runner(io.vertx.example.util.Runner) NetClientOptions(io.vertx.core.net.NetClientOptions) NetSocket(io.vertx.core.net.NetSocket) NetSocket(io.vertx.core.net.NetSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) NetClient(io.vertx.core.net.NetClient)

Aggregations

NetClient (io.vertx.core.net.NetClient)52 NetSocket (io.vertx.core.net.NetSocket)35 Test (org.junit.Test)34 NetServerOptions (io.vertx.core.net.NetServerOptions)24 Vertx (io.vertx.core.Vertx)23 JsonObject (io.vertx.core.json.JsonObject)21 Async (io.vertx.ext.unit.Async)20 Handler (io.vertx.core.Handler)19 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 NetClientOptions (io.vertx.core.net.NetClientOptions)17 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)16 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Before (org.junit.Before)16 Message (io.vertx.core.eventbus.Message)15 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)15 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 After (org.junit.After)15