netty获取玩家chanel_Netty回调与Channel执行流程分析

本文分析了 Netty 中的 Channel 回调事件和执行流程,通过 TestHttpServerHandle 类展示了如何处理客户端请求,包括读取请求、响应内容、处理 favicon.ico 请求等。同时,文章还引入了 CommonUtil 工具类用于日志打印。运行结果中展示了当使用 Postman 调用时,服务器端的回调事件触发情况。
摘要由CSDN通过智能技术生成

在上一篇的基础上修改代码

1、TestHttpServerHandle  类

package com.example.firstexample;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import io.netty.handler.codec.http.*;

import io.netty.util.CharsetUtil;

import java.net.URI;

public class TestHttpServerHandle extends SimpleChannelInboundHandler{

//读取客户端发送过来的请求,并且向客户端返回响应

protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {

CommonUtil.println(httpObject.getClass());

CommonUtil.println(channelHandlerContext.channel().remoteAddress());

if(httpObject instanceof HttpRequest){

HttpRequest httpRequest = (HttpRequest) httpObject;

CommonUtil.println("请求方法名:" + httpRequest.method().name() + ",uri:" + httpRequest.uri());

URI uri = new URI(httpRequest.uri());

if("/favicon.ico".equals(uri.getPath())){

CommonUtil.println("请求favicon.ico");

return;

}

ByteBuf content = Unpooled.copiedBuffer("Hello world", CharsetUtil.UTF_8);

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());

//写回客户端

channelHandlerContext.writeAndFlush(response);

//channelHandlerContext.close();

}

}

@Override

public void channelActive(ChannelHandlerContext ctx) throws Exception {

CommonUtil.println("channel Active");

super.channelActive(ctx);

}

@Override

public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

CommonUtil.println("channel Registered");

super.channelRegistered(ctx);

}

@Override

public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

CommonUtil.println("channel Added");

super.handlerAdded(ctx);

}

@Override

public void channelInactive(ChannelHandlerContext ctx) throws Exception {

CommonUtil.println("channel Inactive");

super.channelInactive(ctx);

}

@Override

public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

CommonUtil.println("channel Unregistered");

super.channelUnregistered(ctx);

}

}

2、增加公共类CommonUtil

public class CommonUtil {

public static void println(String msg){

System.out.println(String.format("[thread-%s] %s",Thread.currentThread().getId(), msg));

}

public static void println(Object msg){

println(msg.toString());

}

}

3、运行结果

使用postman调用

控制台打印如下图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值