案例要求
Server
public class Server {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerInitializer());
//绑定端口
ChannelFuture channelFuture = serverBootstrap.bind(8081).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
ServerHandler
/*
1.SimpleChannelInboundHandler是ChannelInboundHandlerAdapter的子类
2.HttpObject表示客户端和服务器端相互通讯的数据被封装成 HttpObject对象
*/
public class ServerHandler extends SimpleChannelInboundHandler<HttpObject> {
//读取客户端数据
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
//判断msg是不是HttpRequest请求
if (msg instanceof HttpRequest) {
System.out.println("msg的类型" + msg.getClass());
System.out.println("客户端地址" + ctx.channel().remoteAddress());
//过滤掉特定资源
HttpRequest httpRequest = (HttpRequest) msg;
//获取uri
URI uri = new URI(httpRequest.uri());
if ("/favicon.ico".equals(uri.getPath())) {
System.out.println("请求了favicon.ico图标,但是不进行响应");
return;
}
//回复信息给浏览器 【Http协议】
ByteBuf content = Unpooled.copiedBuffer("hello,我是服务器", StandardCharsets.UTF_16);
//构造Http响应 即HttpResponse--指定http版本和状态码
FullHttpResponse response =
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
//将构建好的response返回
ctx.writeAndFlush(response);
}
}
}
ServerInitializer
public class ServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//加入处理器
//得到pipeline
ChannelPipeline pipeline = ch.pipeline();
//加入Netty提供的HttpServerCodec 编解码器
pipeline.addLast("MyHttpServerCodec", new HttpServerCodec());
//加入自定义处理器
pipeline.addLast("MyServerHandler", new ServerHandler());
}
}
效果
注意点
1.http服务器的时候,ServerBootStrap在bind端口之后,可能收不到浏览器请求,可能是由于某些端口(6665-6669等)是浏览器的默认非安全端口,浏览器给拦截了
2.需要对某些资源请求设置不进行访问,比如浏览器页面的小图标
3.不同的浏览器来请求对应着各自的handler,并不是使用同一个,因为HTTP不是长连接,所有刷新一个页面也会再次分配一个新的handler