HttpRequest httpRequest = (HttpRequest) msg;
String uri = httpRequest.uri();
URI uri1 = new URI(uri);
if ("/favicon.ico".equals(uri1.getPath())) {
return;
}
加入这样一段可以不做相对应业务的处理。就是过滤。
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64322
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64322
win10默认的浏览器只发了一次请求
每个浏览器对应一个channel
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpRequest){
System.out.println("msg.getClass() = " + msg.getClass());
System.out.println("ctx.channel().remoteAddress() = " + ctx.channel().remoteAddress());
HttpRequest httpRequest = (HttpRequest) msg;
String uri = httpRequest.uri();
URI uri1 = new URI(uri);
if ("/favicon.ico".equals(uri1.getPath())) {
return;
}
ByteBuf buf = Unpooled.copiedBuffer("我是总部", Charset.forName("gbk"));
DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,buf);//type
defaultFullHttpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
defaultFullHttpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
ctx.writeAndFlush(defaultFullHttpResponse);//type
}
}
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64322
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64322
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64921
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:64921
two
谷歌刷新也是会变,和http协议有关,特点。
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:65185
msg.getClass() = class io.netty.handler.codec.http.DefaultHttpRequest
ctx.channel().remoteAddress() = /127.0.0.1:65185