use of io.netty.handler.codec.string.StringEncoder in project force-oneself by Force-oneself.
the class EventLoopClient method main.
public static void main(String[] args) throws InterruptedException {
ChannelFuture channelFuture = new Bootstrap().group(new NioEventLoopGroup()).channel(NioSocketChannel.class).handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringEncoder());
}
}).connect(new InetSocketAddress("localhost", 8080)).channel().writeAndFlush("Hello, World");
// 处理回调
channelFuture.addListener((ChannelFutureListener) channelFuture1 -> {
Channel channel = channelFuture.channel();
channel.writeAndFlush("hello world");
});
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testSendFrequently.
@Test
public void testSendFrequently() throws InterruptedException {
AtomicInteger counter = new AtomicInteger(0);
int N = 10000;
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Assert.assertTrue(msg instanceof String);
String message = (String) msg;
Assert.assertTrue(message.toLowerCase().contains("ok") || message.toLowerCase().contains("proxy"));
if (message.toLowerCase().contains("ok")) {
String[] strs = message.split("\r\n");
for (String str : strs) {
if (str.toLowerCase().contains("ok")) {
counter.getAndIncrement();
}
}
}
super.channelRead(ctx, msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// Assert.assertEquals(N, counter.get());
logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
System.out.println("Complete" + counter.get());
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
Thread.sleep(1);
for (int i = 0; i < N; i++) {
future.channel().writeAndFlush(new SimpleStringParser("OK").format());
}
}
});
Thread.sleep(1000 * 10);
logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testCloseOnBothSide.
@Test
public void testCloseOnBothSide() throws Exception {
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("[channelRead] msg: {}", msg);
super.channelRead(ctx, msg);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelUnregistered(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
}
});
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().close().sync();
}
});
Thread.sleep(1000 * 3);
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testCloseOnBothSide2.
@Test
public void testCloseOnBothSide2() throws Exception {
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("[channelRead] msg: {}", msg);
ctx.channel().close();
super.channelRead(ctx, msg);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelUnregistered(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
}
});
future.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
logger.info("[close] close future");
}
});
Thread.sleep(1000 * 3);
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class SecureChatServerInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
pipeline.addLast(createSslHandler(initSSLContext()));
// On top of the SSL handler, add the text line codec.
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
// and then business logic.
pipeline.addLast(new SecureChatServerHandler());
}
Aggregations