java - Netty Nio 在 Java 中读取来自 ChannelFuture 的即将到来的消息

标签 java sockets web netty nio

我正在尝试使用 the following code这是 Netty Nio 中网络套接字的实现。我已经实现了一个 JavaFx Gui,我想从 Gui 中读取从服务器或其他客户端接收到的消息。 NettyClient 代码如下:

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?
}

如何使用代码其他部分的回调函数返回的 channelFuture 来读取传入的消息?是否需要再次调用callBack,或者如何接收到 channel 的更新消息?我可以从我的代码(在按钮事件中)使用类似 ret.channel().read() 的东西(以便获取最后一条消息)吗?

最佳答案

通过阅读该代码,NettyClient 用于创建连接(ClientHandler),一旦连接完成,Netty 将调用 ClientHandler.channelActive,如果你想向服务器发送数据,你应该在这里放一些代码。如果此连接从服务器获取消息,Netty 将调用 ClientHandler.channelRead,将您的代码用于处理消息。

您还需要阅读文档以了解 netty 编码器/解码器的工作原理。

  • 如何使用代码其他部分的回调函数返回的 channelFuture 来读取传入的消息?

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

  • 是否需要再次调用callBack,或者如何接收 channel 更新的消息?

    if server message come,ClientHandler.channelRead is called.

  • 我能否在我的代码中(在按钮事件中)使用像 ret.channel().read() 这样的东西(以便获取最后一条消息)?

    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.

最后,你真的需要这么重的库来做网络吗?如果不需要,试试This code ,简单易懂

关于java - Netty Nio 在 Java 中读取来自 ChannelFuture 的即将到来的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47555617/

相关文章:

c - Socket 程序演示网络中的链路层通信,无需使用 IP 地址

java - 用于服务器/客户端通信的自定义 readLine() 方法

jquery - 将 Web 界面添加到嵌入式 QT 应用程序

python - Python3中的Socket,监听端口

java - OOP 设计模式 - 隐式调用 super 方法或其他解决方案

java - 递归二叉搜索树删除方法

java - 如何在不重新启动的情况下删除apache axis tmp文件

html - 在居中图像上居中文本和表单(电子邮件输入和提交按钮)

javascript - 如何从 Google Chart api 获取柱形名称

使用注释的 Java 代码检测