[优化]避免多次创建NettyWebSocketServerHandler

This commit is contained in:
DJX 2023-09-20 12:39:03 +08:00
parent a1a1c4061b
commit d78624caa7
2 changed files with 4 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import javax.annotation.PreDestroy;
@Configuration
public class NettyWebSocketServer {
public static final int WEB_SOCKET_PORT = 8090;
public static final NettyWebSocketServerHandler NETTY_WEB_SOCKET_SERVER_HANDLER = new NettyWebSocketServerHandler();
// 创建线程池执行器
private EventLoopGroup bossGroup = new NioEventLoopGroup(1);
private EventLoopGroup workerGroup = new NioEventLoopGroup(NettyRuntime.availableProcessors());
@ -90,7 +91,7 @@ public class NettyWebSocketServer {
*/
pipeline.addLast(new WebSocketServerProtocolHandler("/"));
// 自定义handler 处理业务逻辑
pipeline.addLast(new NettyWebSocketServerHandler());
pipeline.addLast(NETTY_WEB_SOCKET_SERVER_HANDLER);
}
});
// 启动服务器监听端口阻塞直到启动成功

View File

@ -7,6 +7,7 @@ import com.abin.mallchat.common.user.domain.enums.WSReqTypeEnum;
import com.abin.mallchat.common.user.domain.vo.request.ws.WSAuthorize;
import com.abin.mallchat.common.user.domain.vo.request.ws.WSBaseReq;
import com.abin.mallchat.common.user.service.WebSocketService;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
@ -17,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@Sharable
public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private WebSocketService webSocketService;