暮夜望日 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);
    

    第一句话忽略吧。

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记