mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-13 21:53:41 +08:00
[优化]避免多次创建NettyWebSocketServerHandler
This commit is contained in:
@@ -27,6 +27,7 @@ import javax.annotation.PreDestroy;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class NettyWebSocketServer {
|
public class NettyWebSocketServer {
|
||||||
public static final int WEB_SOCKET_PORT = 8090;
|
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 bossGroup = new NioEventLoopGroup(1);
|
||||||
private EventLoopGroup workerGroup = new NioEventLoopGroup(NettyRuntime.availableProcessors());
|
private EventLoopGroup workerGroup = new NioEventLoopGroup(NettyRuntime.availableProcessors());
|
||||||
@@ -90,7 +91,7 @@ public class NettyWebSocketServer {
|
|||||||
*/
|
*/
|
||||||
pipeline.addLast(new WebSocketServerProtocolHandler("/"));
|
pipeline.addLast(new WebSocketServerProtocolHandler("/"));
|
||||||
// 自定义handler ,处理业务逻辑
|
// 自定义handler ,处理业务逻辑
|
||||||
pipeline.addLast(new NettyWebSocketServerHandler());
|
pipeline.addLast(NETTY_WEB_SOCKET_SERVER_HANDLER);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// 启动服务器,监听端口,阻塞直到启动成功
|
// 启动服务器,监听端口,阻塞直到启动成功
|
||||||
|
|||||||
@@ -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.WSAuthorize;
|
||||||
import com.abin.mallchat.common.user.domain.vo.request.ws.WSBaseReq;
|
import com.abin.mallchat.common.user.domain.vo.request.ws.WSBaseReq;
|
||||||
import com.abin.mallchat.common.user.service.WebSocketService;
|
import com.abin.mallchat.common.user.service.WebSocketService;
|
||||||
|
import io.netty.channel.ChannelHandler.Sharable;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
@@ -17,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Sharable
|
||||||
public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
|
public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
|
||||||
|
|
||||||
private WebSocketService webSocketService;
|
private WebSocketService webSocketService;
|
||||||
|
|||||||
Reference in New Issue
Block a user