简体   繁体   English

用netty动态传输数据

[英]transfer data dynamically with netty

I've been learning Netty for a while, and from the Netty's tutorials(the MEAP book), almostly the examples are based on a fixed framework, like the EventLoop , Bootstrap , it seems that only the implementations of the handlers in the channelPipeline are the things we really should be concerned about. 我学习Netty已有一段时间了,从Netty的教程(MEAP书)中,几乎所有示例都是基于固定框架,例如EventLoopBootstrap ,似乎只有channelPipeline中处理程序的channelPipeline是我们真正应该关注的事情。

Here I wanna design a simple chess game, based on a Server/Client mode, where two players are on different computers. 在这里,我想基于服务器/客户端模式设计一个简单的国际象棋游戏,其中两个玩家在不同的计算机上。 And the background data I want to use Netty to transmit.(I just wanna practice using netty) 还有我想使用Netty传输的背景数据。(我只是想练习使用netty)

And in such a game, the front GUI detect the player put a chessman and then make some change to the data. 在这样的游戏中,前GUI会检测到玩家下了一个国际象棋棋子,然后对数据进行了一些更改。 Then, I need to deliver this data to the other player. 然后,我需要将此数据传递给其他播放器。 And here comes the question. 问题来了。

I don't know how to implement a ChannelHandler in this situation, because in most examples, it seems that the data are not added dynamically by the handler. 我不知道如何在这种情况下实现ChannelHandler ,因为在大多数示例中,处理程序似乎没有动态添加数据。 For instance, the data was created when channel was active by the method channelActive() or something else. 例如,当通道通过方法channelActive()处于活动状态时创建了数据。 All these methods was auto-invoked by Netty itself. 所有这些方法都是由Netty本身自动调用的。

The only method I think is the write() . 我认为唯一的方法是write() However, it seems that I have to call this method by myself if I implements this method, I don't know where I can get the parameter ChannelHandlerContext . 但是,如果实现此方法,似乎我必须自己调用此方法,我不知道在哪里可以获取参数ChannelHandlerContext

So, how to solve problems like this? 那么,如何解决这样的问题呢?


ps I'm not so familiar with java network programming, nor the Netty. ps我不太熟悉Java网络编程,也不太熟悉Netty。 All the things I learn is based on the book, which I haven't finished reading yet. 我学到的所有东西都是基于这本书,我还没有读完。 :) :)

Channel hander of Netty looks Netty的频道处理程序外观

package netty_sample;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

/**
 * Server side action
 */
public class EchoServerHandler extends SimpleChannelHandler {
    /**
     * This method will be invoked when server recieved a message
    */
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) {
        String msg = (String) event.getMessage(); // extract a message received

        // You can write any code which handles the message, changes data, and create message for client, etc. 

        ctx.getChannel().write(someMessageToClient); // send back to client
    }
}

As I understand, handler routine is invoked dynamically (in event-driven) when server received a message. 据我了解,当服务器接收到消息时,处理程序例程是动态调用的(在事件驱动下)。 So code in the handler works dynamically, and you can write anything in the code. 因此,处理程序中的代码是动态工作的,您可以在代码中编写任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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