feat(新增): 🚀 netty开始的服务器端
This commit is contained in:
parent
96b9935b08
commit
83449e7e7d
|
@ -10,7 +10,7 @@
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -106,6 +106,15 @@
|
||||||
<skipDockerBuild>false</skipDockerBuild>
|
<skipDockerBuild>false</skipDockerBuild>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>21</source>
|
||||||
|
<target>21</target>
|
||||||
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package cn.bunny.service.netty;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
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.string.StringDecoder;
|
||||||
|
|
||||||
|
import static java.lang.StringTemplate.STR;
|
||||||
|
|
||||||
|
public class Start01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 启动器
|
||||||
|
new ServerBootstrap()
|
||||||
|
// 添加组件 NioEventLoopGroup WorkerEventLoop
|
||||||
|
.group(new NioEventLoopGroup())
|
||||||
|
// 选择 ServerNio 实现,还支持某种Linux的实现
|
||||||
|
.channel(NioServerSocketChannel.class)
|
||||||
|
// 处理字节进行分工,需要处理哪些逻辑,决定了 child可以执行哪些操作
|
||||||
|
.childHandler(
|
||||||
|
// 代码客户端读写通道 添加别的 handler
|
||||||
|
new ChannelInitializer<NioSocketChannel>() {
|
||||||
|
@Override
|
||||||
|
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
||||||
|
// 添加解码,将传输过来的 ByteBuffer 转为字符串
|
||||||
|
nioSocketChannel.pipeline().addLast(new StringDecoder());
|
||||||
|
// 自定处理器
|
||||||
|
nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
||||||
|
// 发生读事件
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
// 打印上一步转化好的字符串
|
||||||
|
System.out.println(STR."消息为\{msg}");
|
||||||
|
super.channelRead(ctx, msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// 绑定端口
|
||||||
|
.bind(8080);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue