yonge 2 vuotta sitten
vanhempi
commit
4feffbce28

+ 0 - 7
audio-analysis/src/main/java/com/yonge/netty/server/handler/NettyChannelManager.java

@@ -10,19 +10,14 @@ import java.util.concurrent.ConcurrentMap;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import com.ym.mec.util.json.JsonUtil;
-import com.yonge.netty.server.service.UserChannelContextService;
 
 @Component
 public class NettyChannelManager {
 
 	private static final Logger LOGGER = LoggerFactory.getLogger(NettyChannelManager.class);
-	
-	@Autowired
-	private UserChannelContextService userChannelContextService;
 
 	/**
 	 * {@link Channel#attr(AttributeKey)} 属性中,表示 Channel 对应的用户
@@ -89,8 +84,6 @@ public class NettyChannelManager {
 	 */
 	public void remove(Channel channel) {
 		
-		userChannelContextService.remove(channel);
-		
 		// 移除 channels
 		channels.remove(channel.id());
 

+ 11 - 0
audio-analysis/src/main/java/com/yonge/netty/server/handler/NettyServerHandler.java

@@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.oauth2.common.OAuth2AccessToken;
 import org.springframework.stereotype.Component;
 
+import com.yonge.netty.server.service.UserChannelContextService;
+
 @Component
 @ChannelHandler.Sharable
 public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@@ -22,6 +24,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 
 	@Autowired
 	private NettyChannelManager channelManager;
+	
+	@Autowired
+	private UserChannelContextService userChannelContextService;
 
 	@Override
 	public void channelActive(ChannelHandlerContext ctx) {
@@ -31,6 +36,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 
 	@Override
 	public void channelUnregistered(ChannelHandlerContext ctx) {
+		
+		userChannelContextService.remove(ctx.channel());
+		
 		// 从管理器中移除
 		channelManager.remove(ctx.channel());
 	}
@@ -38,6 +46,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 	@Override
 	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
 		LOGGER.error("[exceptionCaught][连接({}) 发生异常]", ctx.channel().id(), cause);
+		
+		userChannelContextService.remove(ctx.channel());
+		
 		// 断开连接
 		ctx.channel().close();
 	}