Netty Nio reads upcoming ChannelFuture posts in Java

I am trying to use the following code , which is an implementation of network sockets in Netty Nio. I am implementing JavaFx Gui and from Gui I want to read messages received from the server or other clients. NettyClient code is as follows:

public static ChannelFuture callBack () throws Exception{ String host = "localhost"; int port = 8080; try { Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new RequestDataEncoder(), new ResponseDataDecoder(), new ClientHandler(i -> { synchronized (lock) { connectedClients = i; lock.notifyAll(); } })); } }); ChannelFuture f = b.connect(host, port).sync(); //f.channel().closeFuture().sync(); return f; } finally { //workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { ChannelFuture ret; ClientHandler obj = new ClientHandler(i -> { synchronized (lock) { connectedClients = i; lock.notifyAll(); } }); ret = callBack(); int connected = connectedClients; if (connected != 2) { System.out.println("The number if the connected clients is not two before locking"); synchronized (lock) { while (true) { connected = connectedClients; if (connected == 2) break; System.out.println("The number if the connected clients is not two"); lock.wait(); } } } System.out.println("The number if the connected clients is two: " + connected ); ret.channel().read(); // can I use that from other parts of the code in order to read the incoming messages? } 

How can I use the returned Future channel from callBack from other parts of my code to read incoming messages? Do I need to call callBack again or how can I get an updated channel message? Can I use something like ret.channel().read() (to receive the last message) from my code (inside the button event)?

+5
source share
1 answer

After reading this code, NettyClient is used to create a connection (ClientHandler), after the connection is completed ClientHandler.channelActive Netty is called, if you want to send data to the server, you should put some code here. if this connection is received by the message form server, ClientHandler.channelRead is called by Netty, put your code to process the message.

You also need to read the document to find out how the netty encoder / decoder works.

  • How can I use the returned Future channel from callBack from other parts of my code to read incoming messages?

    share those ClientHandler created by NettyClient (string NettyClient.java 29)

  • Do I need to call callBack again or how can I get an updated channel message?

    if a server message arrives, ClientHandler.channelRead is called.

  • Is it possible to use something like ret.channel () from my code (inside the button event). read () (to accept the last message)?

    yes, you could, but not be unclean, play with netty, you write callbacks (when the message arrives, when the message is sent ...), wait until netty calls your code, that is: the driver is net, not you .

last, do you really need such a large library for networking? If not, try this code , it's simple, easy to understand

0
source

Source: https://habr.com/ru/post/1273742/


All Articles