use of io.netty.handler.codec.string.StringEncoder in project baseio by generallycloud.
the class NettyClientThread method main.
public static void main(String[] args) throws Exception {
EventLoopGroup group = NettyUtil.newEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new HelloClient());
}
});
System.out.println("################## Test start ####################");
ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
System.out.println(f.isSuccess());
Channel channel = f.channel();
System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
int len = 1024 * 64;
StringBuilder s = new StringBuilder(len);
for (int i = 0; i < len; i++) {
s.append(len % 10);
}
final String msg = s.toString();
Util.exec(() -> {
int i = 0;
for (; ; ) {
// String s = "hello Service! ---> :" + i;
ChannelFuture f1 = channel.writeAndFlush(msg);
Util.sleep(1);
System.out.println(f1.isDone() + "--------" + i);
i++;
}
});
Util.sleep(Integer.MAX_VALUE);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
use of io.netty.handler.codec.string.StringEncoder in project benchmark by seelunzi.
the class ClientIniter method initChannel.
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("stringD", new StringDecoder());
pipeline.addLast("stringC", new StringEncoder());
pipeline.addLast("http", new HttpClientCodec());
pipeline.addLast("chat", new ChatClientHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project benchmark by seelunzi.
the class ServerIniterHandler method initChannel.
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("docode", new StringDecoder());
pipeline.addLast("encode", new StringEncoder());
pipeline.addLast("chat", new ChatServerHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.
the class NettyServerFilter method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline ph = ch.pipeline();
// 以("\n")为结尾分割的 解码器
// ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
// 解码和编码,应和客户端一致
ph.addLast("decoder", new StringDecoder());
ph.addLast("encoder", new StringEncoder());
// 服务端业务逻辑
ph.addLast("handler", new NettyServerHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.
the class NettyClientFilter method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline ph = ch.pipeline();
/*
* 解码和编码,应和服务端一致
* */
// ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
ph.addLast("decoder", new StringDecoder());
ph.addLast("encoder", new StringEncoder());
// 客户端的逻辑
ph.addLast("handler", new NettyClientHandler());
}
Aggregations