Java Netty TCP 示例:如何发送/接收对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24764158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:44:15  来源:igfitidea点击:

Netty TCP example: how do I send/receive objects?

javacastingionettyinstanceof

提问by Thufir

Can I send/receive any sort of object?

我可以发送/接收任何类型的对象吗?

Looking at the method signature, it uses MessageEvent:

查看方法签名,它使用MessageEvent

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

which, according to the API, inherits from Object. So, just cast that whatever type is expected? Or, perhaps, use instanceof?

根据 API,它继承自 Object。那么,只需投射预期的任何类型?或者,也许,使用instanceof?

From the Netty TPC example on wikipedia:

来自维基百科上Netty TPC 示例

package nettyserver;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;


/* Next up is our custom ChannelHandler. You can see that by the name this is an UpstreamHandler.
 * An UpstreamHandler can receive anything that the Server receives, additionally, an DownstreamHandler can
 * capture packets that the server is about to send. However depending on where in the hierarchy level the handler
 * is placed, it can be triggered in different states.
 * |------| |------| |------|
 * | DSH1 |->-("ABCDE")->| DSH2 |->-("BCD")->| DSH3 |->-("DCB")->[WEB]
 * |------| |------| |------|
 *
 * Above are three DownstreamHandlers each one with a specific task.
 * The first (DSH1) DownstreamHandler is the DelimiterBasedFrameDecoder that just output
 * a String "ABCDE" down the stream. The second (DSH2) DownstreamHandler intercepts the output from the
 * previous DownstreamHandler and performs its specific logic on the input which in this case
 * is to remove the vowels. Now the third (DSH3) DownstreamHandler will intercept the outgoing message
 * and it is assigned to reverse the order of the letters. When there's no DonstreamHandlers left in the
 * ChannelPipeline the output will be sent to the client/server.
 *
 * The same principle applies to UpstreamHandlers. If you want to combine the functionality
 * of the SimpleChannelDownstreamHandler and the SimpleChannelUpstreamHandler there is the class called
 * SimpleChannelHandler. The SimpleChannelHandler class implements both the Down- and Up-stream interfaces
 * which allows the handler to manage messages going both ways.
 *
 * In this example, the SimpleChannelUpstreamHandler will be used.
 *
 * */
public class MyMessageHandler extends SimpleChannelUpstreamHandler {

    private Logger logger = Logger.getLogger(MyMessageHandler.class.getSimpleName());

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        /* The messageReceived method is where all the messages that passes this UpstreamHandler
         * will be caught. Below we can use the class MessageEvents getMessage() to retrieve
         * the message it has intercepted. */
        System.out.println("Server :: Received a new message saying: " + e.getMessage());

        /* We can also use the class MessageEvents getChannel() to retrieve the Channel object
         * created by the ChannelFactory we instantiated in the Server class. We can then use the Channel
         * to perform a write() operation on the pipeline (This will go downstream from beginning of the
         * pipeline to the end).
         * It is important that you append a newline separator, '\n', if you want the ChannelBuffer to be
         * cleaned and forwarded from the FrameDelimiter. By appending the 'delimiter' you send the
         * String to its target destination. */
        e.getChannel().write("Hello, client! Your IP is " + e.getRemoteAddress() + "!\n"
                + "We received your message saying: " + e.getMessage() + "\n");

        /* We must not forget to call the super.messageReceived(...) for our superclass. If you do not do this,
         * the message will be stuck in the pipeline. */
        super.messageReceived(ctx, e);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        /* Override method for exceptions. It's good practice to Log the errors that occur in your
         * errors. */
        logger.log(Level.SEVERE, e.getCause().toString());

        /* We always call the method superclass. */
        super.exceptionCaught(ctx, e);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("Server :: " + e.getChannel().getRemoteAddress() + " has connected!");
        /* We can specifically handle new connections.
         * For example add the Channel to a ChannelGroup. */

        /* We always call the method superclass. */
        super.channelConnected(ctx, e);
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        /* We can also handle when a disconnection occur.
         * Here we could remove the Channel from abovementioned ChannelGroup. */
        System.out.println("Server :: " + e.getChannel().getRemoteAddress() + " has disconnected from the Server.");
        /* We always call the method superclass. */
        super.channelDisconnected(ctx, e);
    }

}

Admittedly, this question made more sense when I first woke up, but now that I've written it down...in for a penny.

诚然,当我第一次醒来时,这个问题更有意义,但现在我已经把它写下来了……一分钱。

采纳答案by Norman Maurer