数据发送完成后再关闭连接,解决http1.0数据传输问题

master
fengfei 7 years ago
parent 1fab6118b3
commit 778b616540
  1. 45
      proxy-server/src/main/java/org/fengfei/lanproxy/server/handlers/ServerChannelHandler.java

@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
@ -26,26 +27,26 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler<ProxyMessa
protected void channelRead0(ChannelHandlerContext ctx, ProxyMessage proxyMessage) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, ProxyMessage proxyMessage) throws Exception {
logger.debug("ProxyMessage received {}", proxyMessage.getType()); logger.debug("ProxyMessage received {}", proxyMessage.getType());
switch (proxyMessage.getType()) { switch (proxyMessage.getType()) {
case ProxyMessage.TYPE_HEARTBEAT: case ProxyMessage.TYPE_HEARTBEAT:
handleHeartbeatMessage(ctx, proxyMessage); handleHeartbeatMessage(ctx, proxyMessage);
break; break;
case ProxyMessage.TYPE_AUTH: case ProxyMessage.TYPE_AUTH:
handleAuthMessage(ctx, proxyMessage); handleAuthMessage(ctx, proxyMessage);
break; break;
case ProxyMessage.TYPE_CONNECT: case ProxyMessage.TYPE_CONNECT:
handleConnectMessage(ctx, proxyMessage); handleConnectMessage(ctx, proxyMessage);
break; break;
case ProxyMessage.TYPE_DISCONNECT: case ProxyMessage.TYPE_DISCONNECT:
handleDisconnectMessage(ctx, proxyMessage); handleDisconnectMessage(ctx, proxyMessage);
break; break;
case ProxyMessage.TYPE_TRANSFER: case ProxyMessage.TYPE_TRANSFER:
handleTransferMessage(ctx, proxyMessage); handleTransferMessage(ctx, proxyMessage);
break; break;
case ProxyMessage.TYPE_WRITE_CONTROL: case ProxyMessage.TYPE_WRITE_CONTROL:
handleWriteControlMessage(ctx, proxyMessage); handleWriteControlMessage(ctx, proxyMessage);
break; break;
default: default:
break; break;
} }
} }
@ -74,7 +75,9 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler<ProxyMessa
String userId = proxyMessage.getUri(); String userId = proxyMessage.getUri();
Channel userChannel = ProxyChannelManager.removeUserChannel(ctx.channel(), userId); Channel userChannel = ProxyChannelManager.removeUserChannel(ctx.channel(), userId);
if (userChannel != null) { if (userChannel != null) {
userChannel.close(); // 数据发送完成后再关闭连接,解决http1.0数据传输问题
ByteBuf buf = ctx.alloc().buffer(0);
userChannel.writeAndFlush(buf).addListener(ChannelFutureListener.CLOSE);
} }
} }

Loading…
Cancel
Save