1

服务器代码:

public static void main(String[] args) {
        try {
            ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
            bootstrap.setPipelineFactory(new PipelineFactory());
            bootstrap.bind(new InetSocketAddress("localhost", port));
            System.out.println("Listening on " + port);
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    }

管道:

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.DefaultChannelPipeline;
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;

public class PipelineFactory implements ChannelPipelineFactory {

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = new DefaultChannelPipeline();
        try {
            pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
            pipeline.addLast("messagehandler", new MessageHandler());
        } catch(Exception exception) {
            exception.printStackTrace();
        }
        return pipeline;
    }

}

我在 MessageHandler 类中覆盖了 messageReceiveed 方法:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    try {
        if (!e.getChannel().isConnected()) {
            System.out.println("Not connected to server...");
            return;
        }
        Message message = (Message) e.getMessage();
        queueMessage(message);
        super.messageReceived(ctx, e);
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

我要发送的对象(消息):

public class Message {

    private String text;
    private byte rights;

    public Message(String text, int rights) {
        this.text = text;
        this.rights = (byte) rights;
    }

    public String getText() {
        return this.text;
    }

    public byte getRights() {
        return this.rights;
    }

}

最后我的客户代码:

public static void main(String[] args) {
    try {
        ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
            }
        });
        ChannelFuture cf = bootstrap.connect(new InetSocketAddress("localhost", 5656));
        cf.awaitUninterruptibly();
        Channel channel = cf.getChannel();
        channel.write(new Message("Hello", 0));
        if (channel.isConnected() && cf.isDone()) {
            System.out.println("Message sent!");
        } else {
            System.out.println("Message has not been sent, killing client.");
        }
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

如果我将消息对象更改为日期,所以我编写了一个日期对象,它工作正常。messageReceived 方法实际上没有被调用,因为我尝试在 messageReceived 方法的开头打印一个随机句子并且它没有打印。我是 netty 的新手,所以我现在很无知,我已经尝试了几件事,结果对我目前的情况没有任何影响。我不知道为什么,但它只是不想写消息对象,也许是它的编码部分?即使那样,日期也会很好地发送,所以我很难过,感谢任何帮助,谢谢。

4

1 回答 1

5

您的 Message 类必须实现 java.io.Serializable。

于 2013-06-14T04:29:23.963 回答