关于mina

项目需要在项目中嵌入直播和IM功能,IM决定使用mina来完成

两个比较重要的知识点:加密、解密

与后台自定义的协议内容: 前2位 type 2-4位 版本号码 4-8位 长度,然后就是消息主体

加密:

package com.miduo.financialmanageplanner.live.mina;

import android.util.Log;

import com.google.gson.Gson;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

/**
 * Created by ${huozhenpeng} on 17/5/1.
 * Company : www.miduo.com
 * 编码
 *
 *
 * 前2位 type 2-4位 版本号码 4-8位 长度
 */

public class IMTextProtocolEncoder implements ProtocolEncoder {
    private final Charset charset=Charset.defaultCharset();
    private Gson gson=new Gson();
    @Override
    public void encode(IoSession ioSession, Object o, ProtocolEncoderOutput protocolEncoderOutput) throws Exception {

        if(null==o)
            return;
        if(o instanceof IMMessageReal)
        {
            CharsetEncoder encoder = (CharsetEncoder) ioSession.getAttribute("encoder");
            if (encoder == null) {
                encoder = charset.newEncoder();
                ioSession.setAttribute("encoder", encoder);
            }
            //单位为字节
            IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
            //如果是short,那就是2个byte,如果是int,那就是增加4个byte,即32。
            //消息type
            buf.putShort(((IMMessageReal) o).getType());
            //版本号码
            buf.putShort(((IMMessageReal) o).getVersion());
            String content="";
            if(((IMMessageReal) o).getType()==996)
            {
               content= gson.toJson(((IMMessageReal) o).getImAuthorization());
            }
            else
            {
               content= gson.toJson(((IMMessageReal) o).getImMessage());
            }
            //内容长度
            buf.putInt(content.getBytes().length);
            buf.putString(content,encoder);
            buf.flip();
            Log.e("abc","encode--->"+content);
            protocolEncoderOutput.write(buf);

        }
    }

    @Override
    public void dispose(IoSession ioSession) throws Exception {

    }
}

解密:

比加密难一些

package com.miduo.financialmanageplanner.live.mina;

import android.util.Log;

import com.google.gson.Gson;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

/**
 * Created by ${huozhenpeng} on 17/5/1.
 * Company : www.miduo.com
 */

public class IMTextProtocolDecoder extends CumulativeProtocolDecoder {

    private Gson gson=new Gson();
    /**
     * 前2位 type 2-4位 版本号码 4-8位 长度
     * 相对于ProtocolDecoder来说,有返回值
     *
     * @param ioSession
     * @param protocolDecoderOutput
     * @return true 本次读取已经完整,可以开始下一次读取
     * false 读取尚未结束
     * @throws Exception
     */
    @Override
    protected boolean doDecode(IoSession ioSession, IoBuffer in, ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
        try {
            if (in.remaining() >= 8) {
                short type = in.getShort();
                short version = in.getShort();
                int len = in.getInt();
//                System.out.println("type:" + type + "_version:" + version + "_" + len);
                if (in.remaining() >= len) {
                    byte[] result = new byte[len];
                /*for(int i=0;i<len;i++){
                    result[i]=in.get();
                }*/
                    in.get(result, 0, len);
                    IMMessageReal imMessageReal=new IMMessageReal();
                    imMessageReal.setType(type);
                    imMessageReal.setVersion(version);
                    IMMessage imMessage = gson.fromJson(new String(result, "UTF-8"),IMMessage.class);
                    imMessageReal.setImMessage(imMessage);
                    Log.e("abc","doDecode---->"+imMessage.toString());
                    protocolDecoderOutput.write(imMessageReal);
                    return true;
                } else {
                    in.rewind();
                    return false;
                }

            } else {
                return false;
            }
        } catch (Exception e) {
            Log.e("abc", e.toString());
            return false;
        }

    }


}

其他的没啥了,百度一下就有

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容