简体   繁体   中英

Netty Nio read the upcoming messages from ChannelFuture in Java

I am trying to use the following code which is an implementation of web sockets in Netty Nio. I have implment a JavaFx Gui and from the Gui I want to read the messages that are received from the Server or from other clients. The NettyClient code is like the following:

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 channelFuture from the callBack from other parts of my code in order to read the incoming messages? Do I need to call again callBack, or how can I received the updated message of the channel? Could I possible use from my code (inside a button event) something like ret.channel().read() (so as to take the last message)?

By reading that code,the NettyClient is used to create connection(ClientHandler ),once connect done,ClientHandler.channelActive is called by Netty,if you want send data to server,you should put some code here. if this connection get message form server, ClientHandler.channelRead is called by Netty, put your code to handle message.

You also need to read doc to know how netty encoder/decoder works.

  • How can I use the returned channelFuture from the callBack from other parts of my code in order to read the incoming messages?

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

  • Do I need to call again callBack, or how can I received the updated message of the channel?

    if server message come,ClientHandler.channelRead is called.

  • Could I possible use from my code (inside a button event) something like ret.channel().read() (so as to take the last message)?

    yes you could,but not a netty way,to play with netty,you write callbacks(when message come,when message sent ...),wait netty call your code,that is : the driver is netty,not you.

last,do you really need such a heavy library to do network?if not ,try This code ,it simple,easy to understanding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM