Skip to content

Commit fdc2562

Browse files
committed
浏览器->Netty服务端->Netty客户端->后端服务
1 parent 915ad42 commit fdc2562

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

Week_03/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
1. Http后台结果分两次进入ChannelInboundHandlerAdapter,一次HttpResponse 一次HttpContent
44
2. 后端服务返回ByteBuf 需要使用ByteBuf内部API复制比特数组,否则抛出异常
5-
3. 浏览器请求->Netty服务器->Netty客户端->Http后台 InBound OutBound 思路和顺序不清晰,导致结果无法返回到浏览器,最后还是没实现.
5+
3. 浏览器请求->Netty服务器->Netty客户端->Http后台 Netty客户端代码结构有欠缺 整体逻辑较完整

Week_03/code/java01/week03/outbound/httpclinet4/HttpOutboundHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext
8080
URL uri = new URL(url);
8181
String host = uri.getHost();
8282
int port = uri.getPort();
83-
NettyHttpClient.connect(host,port,inbound);
83+
NettyHttpClient.connect(host,port,inbound,ctx);
8484

8585
} catch (Exception e) {
8686

Week_03/code/java01/week03/outbound/netty4/NettyHttpClient.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.guanqp.java01.week03.outbound.netty4;
22

33
import io.netty.bootstrap.Bootstrap;
4-
import io.netty.channel.ChannelFuture;
5-
import io.netty.channel.ChannelInitializer;
6-
import io.netty.channel.ChannelOption;
7-
import io.netty.channel.EventLoopGroup;
4+
import io.netty.channel.*;
85
import io.netty.channel.nio.NioEventLoopGroup;
96
import io.netty.channel.socket.SocketChannel;
107
import io.netty.channel.socket.nio.NioSocketChannel;
@@ -13,7 +10,7 @@
1310

1411
public class NettyHttpClient {
1512

16-
public static void connect(String host, int port,Object msg) throws Exception {
13+
public static void connect(String host, int port,Object msg,final ChannelHandlerContext ctx) throws Exception {
1714
EventLoopGroup workerGroup = new NioEventLoopGroup();
1815

1916
try {
@@ -28,7 +25,7 @@ public void initChannel(SocketChannel ch) throws Exception {
2825
ch.pipeline().addLast(new HttpResponseDecoder());
2926
//客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
3027
ch.pipeline().addLast(new HttpRequestEncoder());
31-
ch.pipeline().addLast(new NettyHttpClientOutboundHandler());
28+
ch.pipeline().addLast(new NettyHttpClientOutboundHandler(ctx));
3229
}
3330
});
3431

Week_03/code/java01/week03/outbound/netty4/NettyHttpClientOutboundHandler.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.netty.handler.codec.http.FullHttpResponse;
1212
import io.netty.handler.codec.http.HttpContent;
1313
import io.netty.handler.codec.http.HttpResponse;
14-
import io.netty.util.CharsetUtil;
1514

1615
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
1716
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
@@ -20,7 +19,10 @@
2019
public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
2120

2221
HttpResponseFilter filter = new HeaderHttpResponseFilter();
23-
22+
ChannelHandlerContext originCtx;
23+
public NettyHttpClientOutboundHandler(ChannelHandlerContext originCtx){
24+
this.originCtx = originCtx;
25+
}
2426
@Override
2527
public void channelRead(ChannelHandlerContext ctx, Object msg)
2628
throws Exception {
@@ -64,7 +66,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
6466
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
6567
exceptionCaught(ctx, e);
6668
} finally {
67-
ctx.writeAndFlush(response);
69+
originCtx.writeAndFlush(response);
6870
//ctx.close();
6971
}
7072
}

0 commit comments

Comments
 (0)