gpt4 book ai didi

java - 我如何在netty4.x中的channelActive()方法中获取URL

转载 作者:行者123 更新时间:2023-12-02 12:12:14 30 4
gpt4 key购买 nike

网址类似于http://url.com/username。当客户端连接服务器时,我需要验证channelActive中的用户名,这可能吗?

谢谢

public class Handler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg){
//NOT HERE
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
//1. get username
//2. do some queries with username
}

编辑:我尝试使用两个处理程序来获取对象,但仍然无法正常工作。

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new SimpleChannelInboundHandler<Object>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
DB.insert(msg);//insert msg to DB
}
});
pipeline.addLast(new MyHandler());



public class MyHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
DB.get();// get msg from DB
ctx.fireChannelActive();
}

最佳答案

你不能。 /username 是 HTTP 请求的一部分,为了获取它,您需要从 channel 读取字节。如果不从 channel 读取字节,您将无法获取用户名,因为此信息尚不可用。

换句话说 - 仅当您的 channel 处于 Activity 状态并且您从 channel 中读取了一些字节后,您才能获取用户名。

关于java - 我如何在netty4.x中的channelActive()方法中获取URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454905/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com