文章目录
前言
服务端基于Springboot,Netty由pom引入。本文写于整个服务端架构初步完成后,部分细节内容未呈现并且有许多架构的不足之处后续会进行重构修正
# 一、pom引入Netty依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
二、初始化Netty
1.创建实现ChannelInitializer 接口的类
public class WebsocketChatServerInitializer extends
ChannelInitializer<SocketChannel> { //1
@Override
public void initChannel(SocketChannel ch) throws Exception {//2
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(64*1024));
pipeline.addLast(new WebSocketServerProtocolHandler("/hak/ws"));
pipeline.addLast(new TextWebSocketFrameHandler());
}
}
2.在pipeline.addLast(new TextWebSocketFrameHandler());处添加的是自己创建的处理器,如下
public class TextWebSocketFrameHandler extends
SimpleChannelInboundHandler<TextWebSocketFrame> {
public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static ConcurrentHashMap<String,Channel> channelMap = new ConcurrentHashMap<>();
@Override
protected void channelRead0(ChannelHandlerContext ctx,
TextWebSocketFrame msg) throws Exception { // (1)
//System.out.println("接收到text消息..." + msg.text());
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // (2)
Channel incoming = ctx.channel();
channels.writeAndFlush(new TextWebSocketFrame("[SERVER] - " + incoming.remoteAddress() + " 加入"));
channels.add(incoming);
String tag = incoming.id() + "";
channelMap.put(tag,incoming);
System.out.println("Client:"+incoming.remoteAddress() +"加入");
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { // (3)
Channel incoming = ctx.channel();
// Broadcast a message to multiple Channels
channels.writeAndFlush(new TextWebSocketFrame("[SERVER] - " + incoming.remoteAddress() + " 离开"));
System.out.println("Client:"+incoming.remoteAddress() +"离开");
// A closed Channel is automatically removed from ChannelGroup,
// so there is no need to do "channels.remove(ctx.channel());"
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { // (5)
Channel incoming = ctx.channel();
System.out.println("Client:"+incoming.remoteAddress()+"在线");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { // (6)
Channel incoming = ctx.channel();
System.out.println("Client:"+incoming.remoteAddress()+"掉线");
//掉线后清楚本类中两个容器中的通道,然后清除在Room中维护的通道 后续取消清除操作改为将该用户的状态更新为"掉线"
for (Map.Entry<String,Channel> entry:channelMap.entrySet()){
if (entry.getValue().equals(incoming)){
String uid = entry.getKey();
Room.players.remove(uid);
channelMap.remove(uid);
System.out.println((Room.players.toString()));
break;
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
Channel incoming = ctx.channel();
incoming.id();
System.out.println("Client:"+incoming.remoteAddress()+"异常");
// 当出现异常就关闭连接
cause.printStackTrace();
ctx.close();
}
}
这里管理了两个channel容器
一个是Netty建议使用的ChannelGroup channels用于保存所有channel,一个是自己创建的ConcurrentHashMap<String,Channel>用于将用户的id和channel对应起来。用户掉线时,清除掉这两个容器中的channel。(后续会改变这一做法,将清除变更为将用户的状态改成"掉线")
三、创建Netty服务器类,运行Netty服务
public class WebsocketChatServer implements Runnable{
private int port;
public WebsocketChatServer(int port) {
this.port = port;
}
public void run(){
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new WebsocketChatServerInitializer())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
System.out.println("WebsocketChatServer 启动了");
ChannelFuture f = b.bind(port).sync(); // (7)
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("WebsocketChatServer 关闭了");
}
}
public static void main(String[] args) throws Exception {
int port;
port = 8088;
new Thread(new WebsocketChatServer(port)).start();
System.out.println("运行线程2...");
new Thread(new MsgLogic()).start();
}
}