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. 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
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:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.