Ai
1 Star 0 Fork 0

spitman/learnnetty

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ProtobufMultiMsgTestServer.java 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
zhouyijin 提交于 2022-01-24 13:50 +08:00 . byte2long decoder
package org.zyj.io.example.case7;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.zyj.io.example.codec.protobuf.MyData;
import java.util.concurrent.ExecutionException;
/**
*
*/
class ProtobufMultiMsgTestServer {
public static void main(String[] args) throws InterruptedException, ExecutionException {
final NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
final NioEventLoopGroup workGroup = new NioEventLoopGroup(4);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap //
.group(bossGroup, workGroup)//
.channel(NioServerSocketChannel.class) //
.option(ChannelOption.SO_BACKLOG, 128) //
.option(ChannelOption.SO_KEEPALIVE, true) //
.handler(new LoggingHandler(LogLevel.INFO)) //
.childHandler(new ChannelInitializer<NioSocketChannel>() { //配置 pipeline
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline() //配置pipeline
.addLast(new ProtobufDecoder(MyData.MyMessage.getDefaultInstance()))//
// .addLast(new ProtobufServerHandler()); //
.addLast(new ProtobufMultiMsgServerHandler()); //
}
});
ChannelFuture channelFuture = serverBootstrap.bind(9998).sync();//
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
System.out.println("服务器启动: 端口监听成功");
} else {
System.out.println("服务器启动: 端口监听失败");
}
}
});
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/spitman/learnnetty.git
git@gitee.com:spitman/learnnetty.git
spitman
learnnetty
learnnetty
master

搜索帮助