How does vertx netsocket return value?

69 views
Skip to first unread message

蓝鲸鱼

unread,
Sep 27, 2016, 2:05:36 AM9/27/16
to vert.x
Sorry, i have asked four same question. 
Because my country network  ,  i can't use google group for long time. 

i have a socket. and i don't know how to return value.

NetClient netClient = vertx.createNetClient();

netClient.connect(port, host, ar -> {

    if (ar.succeeded()) {

        socket = ar.result();

        socket.handler(this::onDataReceived);

        socket.write(buffer);

    }

});


private void onDataReceived(Buffer buffer) {

    // i want to use this method return value. so i can get the socket response.

}


the socket handler can not use return value or  message.reply thing.

Because i want to expose this data to the web.

Paulo Lopes

unread,
Sep 27, 2016, 3:29:53 AM9/27/16
to vert.x
the method onDataReceived will be called with the data as it arrives from the socket, it does not need to return anything, the "response" chunks are in the parameter buffer.

蓝鲸鱼

unread,
Sep 27, 2016, 4:31:29 AM9/27/16
to vert.x
But I want to expose that buffer data to the restful http api.


在 2016年9月27日星期二 UTC+8下午3:29:53,Paulo Lopes写道:

蓝鲸鱼

unread,
Sep 27, 2016, 4:33:00 AM9/27/16
to vert.x
Or what should I use that buffer data .  Like show it on a client (gui or web thing.).

在 2016年9月27日星期二 UTC+8下午4:31:29,蓝鲸鱼写道:

Julien Viet

unread,
Sep 27, 2016, 4:38:30 AM9/27/16
to ve...@googlegroups.com
you should put this buffer in the Vert.x shared data local map and lookup this buffer from your rest endpoint

-- 
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/00a060fa-fad7-45bf-afb7-99ec3ae345df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paulo Lopes

unread,
Sep 27, 2016, 9:37:52 AM9/27/16
to vert.x
Hi i guess you're looking for something like:

router.route("/api").handler(ctx -> {

  NetClient netClient = vertx.createNetClient();

  netClient.connect(port, host, ar -> {

    if (ar.succeeded()) {

        socket = ar.result();

        socket.handler(buff -> {

          ctx.response().write(buff);

        });

        socket.endHandler( v -> {

          ctx.response.end();

        });

        socket.write(buffer);

    }

  });

});

So you can chain the calls from the web -> netclient -> back to web browser
Reply all
Reply to author
Forward
0 new messages