简体   繁体   English

Netty 3.9客户端未发送编码器

[英]Netty 3.9 client not sending encoder

I'm having an issue using Netty 3.9, where i have made a client that sends/executes an encoder as soon as it connects to the server. 我在使用Netty 3.9时遇到问题,我在该客户端中建立了一个在连接到服务器后立即发送/执行编码器的客户端。 But, it just connects without sending the encoder. 但是,它只是连接而没有发送编码器。

ClientHandler 客户端处理程序

public final class ClientHandler extends IdleStateAwareChannelUpstreamHandler {

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        Channel channel = ctx.getChannel();
        logger.info("Channel connected: " + channel);
    }
}

ClientPipelineFactory ClientPipelineFactory

private final ClientHandler handler = new ClientHandler();

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    pipeline.addLast("handler", handler);
    pipeline.addLast("encoder", new HandshakeEncoder());
    return pipeline;
}

HandshakeEncoder 握手编码器

public final class HandshakeEncoder extends OneToOneEncoder {

    @Override
    protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
        ChannelBuffer buffer = ChannelBuffers.buffer(1);

        buffer.writeByte(49);
        return buffer;
    }
}

The encoder will only be called when you write something to the Channel. 仅当您向通道中写入内容时,才会调用编码器。 I guess what you want is to extend SimpleChannelUpstreamHandler and use something like: 我猜你想要的是扩展SimpleChannelUpstreamHandler并使用类似的东西:

public final class HandshakeHandler extends SimpleChannelUpstreamHandler {

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        Channel channel = ctx.getChannel();
        ChannelBuffer buffer = ChannelBuffers.buffer(1);
        buffer.writeByte(49);
        channel.write(buffer);
    }
}

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

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