question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle the receive message sent by server from my main function

See original GitHub issue

Expected behavior

I am trying to figure out, how can i use the received message sent by server to the client from my client’s main method. For example I am using the following example code which exchange informatino from the server to the client https://github.com/eugenp/tutorials/tree/master/libraries/src/main/java/com/baeldung/netty. How exactly can I manipulate my received data? I want to use those data in order to depict them from a javafx gui i have in the client side.

Minimal yet complete reproducer code (or URL to code)

Netty version

4.0.0

JVM version (e.g. java -version)

1.8.0_151

OS version (e.g. uname -a)

Windows 7

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rkapsicommented, Oct 19, 2017
  1. Pass some kind of callback into your ClientHandler. Netty’s Promise would be a fine choice.
class ClientHandler {
   private final Promise<Object> promise;

  ClientHandler(Promise<Object> promise) {
    this.promise = promise;
  }

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    promise.trySuccess(msg);
    ctx.close();
  }
}
  1. In your main() method…
public static void main(String[] args) {
  EventLoopGroup workerGroup = new NioEventLoopGroup();
  Promise<Object> promise = workerGroup.next().newPromise();

  b.handler(new ChannelInitializer<SocketChannel>() {
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
      ch.pipeline().addLast(new RequestDataEncoder(), new ResponseDataDecoder(), new ClientHandler(promise));
    }
  });

  ChannelFuture f = b.connect(host, port).sync();

  // Here's your message
  Object msg = promsie.get();

  f.channel().closeFuture().sync();
}
0reactions
normanmaurercommented, Oct 25, 2017

@kristosh please use stack overflow for questions, this is an issue tracker

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSockets - Send & Receive Messages - Tutorialspoint
Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage...
Read more >
Send and receive message on both ends - Stack Overflow
The server code above can receive and send message from and to the client. The client also can receive and send message from...
Read more >
Using server-sent events - Web APIs | MDN
This code listens for incoming messages (that is, notices from the server that do not have an event field on them) and appends...
Read more >
Python Program that Sends And Receives Message from Client
Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the second one is SOCK_STREAM. AF_INET...
Read more >
HTML5 Server-Sent Events and Examples - Sweetcode.io
event: the event type, such as message, or another defined by your ... The onmessage() function is implemented to handle SSE events that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found