本文整理汇总了Java中io.netty.handler.codec.string.StringEncoder类的典型用法代码示例。如果您正苦于以下问题:Java StringEncoder类的具体用法?Java StringEncoder怎么用?Java StringEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StringEncoder类属于io.netty.handler.codec.string包,在下文中一共展示了StringEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LoggingHandler());
// 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.
if (sslCtx != null)
pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));
// 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 SecureChatClientHandler());
}
示例2: serverBootStrapWithOptionsTest
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@Test
public void serverBootStrapWithOptionsTest() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
LinkedHashMap<String, Object> channelHandlerOptions = new LinkedHashMap<String, Object>();
channelHandlerOptions.put("lineFrame", new LineBasedFrameDecoder(2000));
channelHandlerOptions.put("decoder", new StringDecoder());
channelHandlerOptions.put("encoder", new StringEncoder());
channelHandlerOptions.put("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("Message Received and forward to ConsumerProcessor. Msg -> {}", msg);
}
});
Server server = BootStrap.builder()
.port(5252)
.options(channelHandlerOptions)
.messageConsumer(msg -> log.info(msg))
.build();
assertNotNull(server);
}
示例3: EchoClient
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
public EchoClient(String host, int port) {
EventLoopGroup worker = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(worker)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast(new StringDecoder())
.addLast(new StringEncoder())
.addLast(ech);
}
});
b.connect(host, port);
}
示例4: start
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
public void start() {
ServerBootstrap b = new ServerBootstrap();
b.group(workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
System.out.println("New client connected! (" + socketChannel.localAddress() + ")");
socketChannel.pipeline().addLast(new StringEncoder()).addLast(new StringEncoder()).addLast(new EchoServerHandler());
}
});
f = b.bind(port);
}
示例5: initServer
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
/**
* Start WebImageViewer.
* @param fsimage the fsimage to load.
* @throws IOException if fail to load the fsimage.
*/
@VisibleForTesting
public void initServer(String fsimage)
throws IOException, InterruptedException {
final FSImageLoader loader = FSImageLoader.load(fsimage);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpRequestDecoder(),
new StringEncoder(),
new HttpResponseEncoder(),
new FSImageHandler(loader, allChannels));
}
});
channel = bootstrap.bind(address).sync().channel();
allChannels.add(channel);
address = (InetSocketAddress) channel.localAddress();
LOG.info("WebImageViewer started. Listening on " + address.toString() + ". Press Ctrl+C to stop the viewer.");
}
示例6: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllowHalfClosure(true);
ChannelPipeline pipeline = ch.pipeline();
//IdleStateHandler 与客户端链接后,根据超出配置的时间自动触发userEventTriggered
//readerIdleTime服务端长时间没有读到数据,则为读空闲,触发读空闲监听,并自动关闭链路连接,周期性按readerIdleTime的超时间触发空闲监听方法
//writerIdleTime服务端长时间没有发送写请求,则为空闲,触发写空闲监听,空闲期间,周期性按writerIdleTime的超时间触发空闲监听方法
//allIdleTime 服务端在allIdleTime时间内未接收到客户端消息,或者,也未去向客户端发送消息,则触发周期性操作
pipeline.addLast("ping", new IdleStateHandler(10, 20, 35, TimeUnit.SECONDS));
// 以("\n")为结尾分割的 解码器
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
// 字符串解码 和 编码
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// 自己的逻辑Handler
pipeline.addLast("handler", new DataServerHandler(nodeInfo));
}
示例7: connect
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
public void connect(String host, int port) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new FixedLengthFrameDecoder(1<<5));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture future = b.connect(host, port).sync();
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
示例8: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()))
.addLast("decoder", new StringDecoder())
.addLast("encoder", new StringEncoder())
.addLast("json_to_ob",new JsonToObjectHandler())
.addLast("register",new RegisterHandler())
.addLast("authority", new AuthorityHandler())
.addLast("enterGroup",new EnterGroupHandler())
.addLast("channelManager", new ChannelManagerHandler())
.addLast("createGroup", new CreateGroupHandler())
.addLast("addGroup", new AddGroupHandler())
.addLast("deleteGroup",new DeleteGroupHandler())
.addLast("Limiter", new LimiterHandler())
.addLast("log", new LoggerHandler())
.addLast("response", new Responser());
}
示例9: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/*
* 这个地方的 必须和服务端对应上。否则无法正常解码和编码
*
* 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
*/
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// 客户端的逻辑
pipeline.addLast("handler", new HelloClientHandler());
}
示例10: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@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(sslCtx.newHandler(ch.alloc()));
// 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());
}
示例11: initChannel
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
@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(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));
// 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 SecureChatClientHandler());
}
示例12: main
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
EventLoopGroup group = new OioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(RxtxChannel.class)
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
public void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(
new LineBasedFrameDecoder(32768),
new StringEncoder(),
new StringDecoder(),
new RxtxClientHandler()
);
}
});
ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
示例13: service
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
public static void service() throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new TcpServerHandler());
}
});
ChannelFuture f = bootstrap.bind(IP, PORT).sync();
f.channel().closeFuture().sync();
System.out.println("TCP服务器已启动");
}
示例14: getBootstrap
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
/**
* 初始化Bootstrap
* @return
*/
public static final Bootstrap getBootstrap(){
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class);
b.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel 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 TcpClientHandler());
}
});
b.option(ChannelOption.SO_KEEPALIVE, true);
return b;
}
示例15: run
import io.netty.handler.codec.string.StringEncoder; //导入依赖的package包/类
protected static void run() throws Exception {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public 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(new TcpServerHandler());
}
});
b.bind(IP, PORT).sync();
System.out.println("TCP服务器已启动");
}