暮夜望日 2016-04-01 07:28 采纳率: 100%
浏览 2263
已采纳

mina心跳的实现和业务逻辑之间的问题

 @Override
    protected boolean doDecode(IoSession session, IoBuffer buffer,
            ProtocolDecoderOutput out) throws Exception {
        int position = buffer.position();
        //第一次读取数据的时候
        if(session.getAttribute("fileName")==null){
            session.setAttribute("fileName",buffer.getInt());
            session.setAttribute("fileContent", buffer.getInt());
            return false;
        }
        else{
            int fileName = (Integer) session.getAttribute("fileName");
            int fileContent = (Integer) session.getAttribute("fileContent");
            //数据全部传输完,开始解码
            if(buffer.limit()-buffer.position()>=fileContent+fileName){
                byte[] nameByte = new byte[fileName]; 
                byte[] contentByte = new byte[fileContent]; 
                PictureBean picture = new PictureBean();
                int start = buffer.position();
                int limit = buffer.limit();
                buffer.limit(start+fileName);
                buffer.slice().get(nameByte);
                picture.setFileName(nameByte);
                buffer.position(start+fileName);
                buffer.limit(limit);
                buffer.slice().get(contentByte);
                picture.setFileContent(contentByte);
                session.removeAttribute("fileName");
                session.removeAttribute("fileContent");
                out.write(picture);
                return true;
            }
            buffer.position(position);
            return false;
        }
}

刚刚学mina,用mina来传输文件可以了,客户端的encoder和服务端的Decoder是自定义的。
但是在想加入心跳的时候发现,心跳的内容会和业务的内容冲突,就是解码器那里很难把这两个完全分开。没用XMPP协议。想问下怎么不影响业务的情况下实现心跳

  • 写回答

1条回答 默认 最新

  • 风聆水净 2016-04-01 08:28
    关注

    心跳包内容是固定的啊。在解码完成后,可以判断出来了。
    我记得可以添加心跳类,里面有判断吧。

     KeepAliveMessageFactory heartBeatFactory = new IMKeepAlive();
            // 设置时间
            KeepAliveFilter keepAliveFilter = new KeepAliveFilter(heartBeatFactory,
                    IdleStatus.BOTH_IDLE, KeepAliveRequestTimeoutHandler.CLOSE,
                    IDLE_TIME, TIME_OUT);
    

    第一句话忽略吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况