请选择 进入手机版 | 继续访问电脑版

默认
发表评论 5
想开发IM:买成品怕坑?租第3方怕贵?找开源自已撸?尽量别走弯路了... 找站长给点建议
新手求助: Netty中获取Attribute的时候返回空的问题
client给channel设置了一个attr, 但是再server端通过ChannelHandlerContext获取attr的时候一直取不到发的字符串啥的都能取到, 找了好久没找到原因


这个是client启动类
package com.linkin.netty.nov.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap clientBootstrap = new Bootstrap();
        try {

            clientBootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .remoteAddress("localhost", 8888)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ClientInitHandler());
                        }

                    });
            ChannelFuture f = clientBootstrap.connect().sync();

            f.channel().closeFuture().sync();
            f.addListener((ChannelFutureListener) future -> {

                boolean isCancellable = future.isCancellable();
                if (isCancellable) {
                    System.out.println("Main ChannelFutureListener isCancellable");
                }

                boolean isCancelled = future.isCancelled();
                if (isCancelled) {
                    System.out.println("Main ChannelFutureListener isCancelled");
                }

                boolean isDone = future.isDone();
                if (isDone) {
                    System.out.println("Main ChannelFutureListener isDone");
                }

                boolean isSuccess = future.isSuccess();
                if (isSuccess) {
                    System.out.println("Main ChannelFutureListener isSuccess");
                }

                boolean isVoid = future.isVoid();
                if (isVoid) {
                    System.out.println("Main ChannelFutureListener isVoid");
                }

            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

}



这个是Server的启动类
package com.linkin.netty.nov.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Server {

    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .localAddress(8888)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        System.out.println("---Server init success---");
                        ch.pipeline().addLast(new ServerInitHandler());
                    }
                });
        try {
            ChannelFuture f = serverBootstrap.bind().sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}



这里是client的channelHandler
package com.linkin.netty.nov.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import io.netty.util.AttributeMap;
import io.netty.util.CharsetUtil;

@Sharable
public class ClientInitHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel handler added");

    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel registered");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().attr(AttributeKey.<String>valueOf("user")).set("shijie");
        System.out.println("---Client channel active");
        Channel channel = ctx.channel();
        channel.writeAndFlush(Unpooled.copiedBuffer("hello-world", CharsetUtil.UTF_8));
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel readComplete");

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel inactive");

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel unregistered");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel handler removed");

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("---Client channel read: " + in.toString(CharsetUtil.UTF_8));
        Thread.sleep(2000);
        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(in));
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("---Client channel writeability changed");

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("---Client channel exceptionCaught");
        System.out.println(cause.getMessage());

    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("---Client channel userEventTriggerd");

    }

}



这个是server的channelHandler
package com.linkin.netty.nov.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;

public class ServerInitHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Attribute<String> attr = ctx.channel().attr(AttributeKey.<String>valueOf("user"));
        String object = attr.get();
        System.out.println(object);
        ByteBuf in = (ByteBuf) msg;

        System.out.println("chennel read Server 收到: " + in.toString(CharsetUtil.UTF_8));
        ctx.writeAndFlush(Unpooled.copiedBuffer(in));
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("----------Server Channel Active");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        System.out.println("---------Channel 读取完成---------");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server channel status : channelInactive");
        super.channelInactive(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server channel status : channelUnregistered");
        super.channelUnregistered(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server channel status : handlerRemoved");
        super.handlerRemoved(ctx);
    }

}



即时通讯网 - 即时通讯开发者社区! 来源: - 即时通讯开发者社区!

上一篇:求教IM开发中的会话数据存储和事件触发下一篇:求教IM离线消息,单聊和群聊是存一张表好还是分开存?
推荐方案
评论 5

你不如直接去读 MobileIMSDK 的服务端代码:https://github.com/JackJiang2011/MobileIMSDK/stargazers,MobileIMSDK里用的Netty的Attribite用的很正常,并没有遇到你说的问题
签名: 【杭州工作】阿里钉钉招后端研发(C++/JAVA)、招2人、截止23/03/31,http://www.52im.net/thread-4117-1-1.html
引用:JackJiang 发表于 2022-12-01 18:11
你不如直接去读 MobileIMSDK  的服务端代码:https://github.com/JackJiang2011/MobileIMSDK/stargazers ...

好嘞
签名: 唉麻了
Client的ChannelHandlerContext跟Server的ChannelHandlerContext不是同一个, 所以Server才没办法获取到Client设置的Attribute
签名: 唉麻了
引用:shijie7 发表于 2022-12-01 18:25
Client的ChannelHandlerContext跟Server的ChannelHandlerContext不是同一个, 所以Server才没办法获取到Clie ...

客户用不上这个,要用也是服务端用
签名: 【杭州工作】阿里钉钉招后端研发(C++/JAVA)、招2人、截止23/03/31,http://www.52im.net/thread-4117-1-1.html
AttributeMap 绑定 Channel ,AttributeMap可以被共享
AttributeMap 绑定 ChannelHandlerContext,AttributeMap不共享
各自的 AttributeMap 互不相干,各不影响
打赏楼主 ×
使用微信打赏! 使用支付宝打赏!

返回顶部