From 57d97bbcdac3fab70569fa67ac9b01694af3c302 Mon Sep 17 00:00:00 2001 From: lizhanwei <Dana_Lee1016@126.com> Date: 星期一, 17 二月 2020 13:34:28 +0800 Subject: [PATCH] 修改NettyTcp;1.增加ByteUtil方法;2,增加服务端通用消息应答;3.增加客户端通用消息应答;4.服务器端消息封装;5,所有平台消息重构放入专用包下;6.所有消息打印TAG为PlatFormMessage 7.增加客户端获取学员信息、鉴权消息 8.MessageProcessor增加解析转义、验证之后的服务消息 --- im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java | 33 ++++ app/src/main/java/safeluck/drive/evaluation/platformMessage/GainStuMessage.java | 58 ++++++++ app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerCommonRsp.java | 52 +++++++ app/src/main/java/safeluck/drive/evaluation/platformMessage/StartExamMessage.java | 2 app/src/main/java/safeluck/drive/evaluation/platformMessage/KeepaliveMessage.java | 4 app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterMessage.java | 4 app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterResp.java | 35 +++++ app/src/main/java/safeluck/drive/evaluation/platformMessage/AuthMessage.java | 49 +++++++ im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java | 4 app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java | 15 ++ app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerProtocol.java | 51 +++++++ /dev/null | 28 ---- app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java | 2 app/src/main/java/safeluck/drive/evaluation/platformMessage/PlatFormConstant.java | 7 + app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java | 2 app/src/main/java/safeluck/drive/evaluation/platformMessage/ClientCommonRsp.java | 64 +++++++++ app/src/main/java/safeluck/drive/evaluation/platformMessage/DriveExamProtocol.java | 2 im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | 6 app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java | 2 19 files changed, 377 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/GainStuMessage.java b/app/src/main/java/safeluck/drive/evaluation/bean/GainStuMessage.java deleted file mode 100644 index b31d4fa..0000000 --- a/app/src/main/java/safeluck/drive/evaluation/bean/GainStuMessage.java +++ /dev/null @@ -1,28 +0,0 @@ -package safeluck.drive.evaluation.bean; - -/** - * 鑾峰彇瀛﹀憳淇℃伅娑堟伅锛堝彂閫佽韩浠借瘉ID涓婂幓缁欏钩鍙帮級 - * MyApplication2 - * Created by lzw on 2019/12/19. 18:26:35 - * 閭锛�632393724@qq.com - * All Rights Saved! Chongqing AnYun Tech co. LTD - */ -public class GainStuMessage extends DriveExamProtocol { - - private static final int BODY_LENGTH = 0; - - public GainStuMessage(short msg_id) { - super(msg_id); - } - - @Override - protected short msgBodyLength() { - return BODY_LENGTH; - } - - @Override - protected byte[] createMessageBody() { - - return new byte[BODY_LENGTH]; - } -} diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java index 056a296..a508360 100644 --- a/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSClientBootstrap.java @@ -101,7 +101,7 @@ return null; } - public void sendMessage(String message){ + public void sendMessage(byte[] message){ if (isActive){ imsClient.sendMsg(message); } diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java index b2d6612..27afae5 100644 --- a/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java @@ -2,7 +2,7 @@ import com.anyun.im_lib.listener.OnEventListener; -import safeluck.drive.evaluation.bean.RegisterMessage; +import safeluck.drive.evaluation.platformMessage.RegisterMessage; /** * MyApplication2 diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java b/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java index 006aece..1f3059c 100644 --- a/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java +++ b/app/src/main/java/safeluck/drive/evaluation/im/IMessageProcessor.java @@ -8,5 +8,5 @@ */ public interface IMessageProcessor { void receiveMsg(byte[] message); - void sendMessage(String msg); + void sendMessage(byte[] msg); } diff --git a/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java index 450aac7..a38cf10 100644 --- a/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java +++ b/app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java @@ -4,6 +4,7 @@ import com.anyun.im_lib.util.ByteUtil; +import safeluck.drive.evaluation.platformMessage.RegisterResp; import safeluck.drive.evaluation.util.CThreadPoolExecutor; import safeluck.drive.evaluation.util.Utils; @@ -36,11 +37,23 @@ byte checkcolde=Utils.calCheckCode(ByteUtil.subArray(datas,0,datas.length-1)); if (checkcolde == datas[datas.length-1]){ Log.i(TAG, "receiveMsg: 娑堟伅姝g‘"); + parseMessage(datas); + } + } + + private void parseMessage(byte[] datas) { + byte[] msgidBytes = ByteUtil.subArray(datas,1,2); + short msgid = ByteUtil.getShort(msgidBytes); + + switch (msgid){ + case (short) 0x8100: + RegisterResp registerResp = new RegisterResp(datas); + break; } } @Override - public void sendMessage(final String msg) { + public void sendMessage(final byte[] msg) { CThreadPoolExecutor.runInBackground(new Runnable() { @Override public void run() { diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/AuthMessage.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/AuthMessage.java new file mode 100644 index 0000000..55123fd --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/AuthMessage.java @@ -0,0 +1,49 @@ +package safeluck.drive.evaluation.platformMessage; + +import android.util.Log; + +import com.anyun.im_lib.util.ByteUtil; + +public class AuthMessage extends DriveExamProtocol { + + private static final String TAG = "AuthMessage"; + private static final int BODY_LENGTH = 12; + private long timeStamp; + private String hexStrPwd; + + public String getHexStrPwd() { + return hexStrPwd; + } + + public void setHexStrPwd(String hexStrPwd) { + this.hexStrPwd = hexStrPwd; + } + + /** + * 鏋勯�犲嚱鏁� + * + * @param msg_id 娑堟伅ID + */ + public AuthMessage(short msg_id) { + super(msg_id); + } + + @Override + protected byte[] createMessageBody() { + byte[] messageBody = new byte[BODY_LENGTH]; + int pos = 0; + timeStamp = System.currentTimeMillis(); + Log.i(TAG, "createMessageBody: timeStamp="+timeStamp); + byte[] timeStampBytes = ByteUtil.intGetBytes((int)timeStamp); + System.arraycopy(timeStampBytes,0,messageBody,pos,timeStampBytes.length); + pos+= 4; + byte [] hexPwdBytes = ByteUtil.hexStr2Bytes(hexStrPwd); + System.arraycopy(hexPwdBytes,0,messageBody,pos,hexPwdBytes.length); + return messageBody; + } + + @Override + protected short msgBodyLength() { + return BODY_LENGTH; + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/ClientCommonRsp.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ClientCommonRsp.java new file mode 100644 index 0000000..8fc2cac --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ClientCommonRsp.java @@ -0,0 +1,64 @@ +package safeluck.drive.evaluation.platformMessage; + +import com.anyun.im_lib.util.ByteUtil; + +public class ClientCommonRsp extends DriveExamProtocol { + private static final int BODY_LENGTH = 5; + + private short result;//缁撴灉 + private short msgId;//瀵瑰簲鐨勬湇鍔″櫒绔秷鎭痠d + private short msg_serial;//鏈嶅姟鍣ㄦ秷鎭粰鐨勬祦姘村彿 + + private int curPos = 0; + + /** + * 鏋勯�犲嚱鏁� + * + * + */ + public ClientCommonRsp() { + super((short) 0x0001); + } + + @Override + protected byte[] createMessageBody() { + byte[] messageBody = new byte[BODY_LENGTH]; + byte[] msg_serialBytes= ByteUtil.shortGetBytes(msg_serial); + byte[] msgidBytes = ByteUtil.shortGetBytes(msgId); + System.arraycopy(msg_serialBytes,0,messageBody,curPos,2); + curPos += 2; + System.arraycopy(msgidBytes,0,messageBody,curPos,2); + curPos += 2; + System.arraycopy(ByteUtil.shortGetByte(result),0,messageBody,curPos,1); + return messageBody; + } + + @Override + protected short msgBodyLength() { + return BODY_LENGTH; + } + + public short getResult() { + return result; + } + + public void setResult(short result) { + this.result = result; + } + + public short getMsgId() { + return msgId; + } + + public void setMsgId(short msgId) { + this.msgId = msgId; + } + + public short getMsg_serial() { + return msg_serial; + } + + public void setMsg_serial(short msg_serial) { + this.msg_serial = msg_serial; + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/DriveExamProtocol.java similarity index 98% rename from app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java rename to app/src/main/java/safeluck/drive/evaluation/platformMessage/DriveExamProtocol.java index ffd0b35..187deaa 100644 --- a/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/DriveExamProtocol.java @@ -1,4 +1,4 @@ -package safeluck.drive.evaluation.bean; +package safeluck.drive.evaluation.platformMessage; import android.util.Log; diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/GainStuMessage.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/GainStuMessage.java new file mode 100644 index 0000000..e40f0e0 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/GainStuMessage.java @@ -0,0 +1,58 @@ +package safeluck.drive.evaluation.platformMessage; + +import android.text.TextUtils; + +import com.anyun.im_lib.util.ByteUtil; + +/** + * 鑾峰彇瀛﹀憳淇℃伅娑堟伅锛堝彂閫佽韩浠借瘉ID涓婂幓缁欏钩鍙帮級 + * MyApplication2 + * Created by lzw on 2019/12/19. 18:26:35 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class GainStuMessage extends DriveExamProtocol { + + private static final int BODY_LENGTH = 17; + + private String id;//鍗D + private short examaCourse = 0; + + public GainStuMessage(short msg_id) { + super(msg_id); + } + + @Override + protected short msgBodyLength() { + return BODY_LENGTH; + } + + @Override + protected byte[] createMessageBody() { + int pos = 0; + byte[] messageBody = new byte[BODY_LENGTH]; + byte[] idBytes = !TextUtils.isEmpty(id)?id.getBytes():"".getBytes(); + System.arraycopy(idBytes,0,messageBody,pos,idBytes.length); + pos+= 16; + byte[] examBytes = ByteUtil.shortGetByte(examaCourse); + System.arraycopy(examBytes,0,messageBody,pos,examBytes.length); + + return messageBody; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public short getExamaCourse() { + return examaCourse; + } + + public void setExamaCourse(short examaCourse) { + this.examaCourse = examaCourse; + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/KeepaliveMessage.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/KeepaliveMessage.java similarity index 82% rename from app/src/main/java/safeluck/drive/evaluation/bean/KeepaliveMessage.java rename to app/src/main/java/safeluck/drive/evaluation/platformMessage/KeepaliveMessage.java index e1fc59f..073fb1d 100644 --- a/app/src/main/java/safeluck/drive/evaluation/bean/KeepaliveMessage.java +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/KeepaliveMessage.java @@ -1,4 +1,6 @@ -package safeluck.drive.evaluation.bean; +package safeluck.drive.evaluation.platformMessage; + +import safeluck.drive.evaluation.platformMessage.DriveExamProtocol; /** * 蹇冭烦娑堟伅 diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/PlatFormConstant.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/PlatFormConstant.java new file mode 100644 index 0000000..7a02022 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/PlatFormConstant.java @@ -0,0 +1,7 @@ +package safeluck.drive.evaluation.platformMessage; + +public class PlatFormConstant { + public static final String TAG = "PlatFormMessage"; + + public static final int SUCCESS = 0; +} diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/RegisterMessage.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterMessage.java similarity index 96% rename from app/src/main/java/safeluck/drive/evaluation/bean/RegisterMessage.java rename to app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterMessage.java index dbfff02..47a6ef5 100644 --- a/app/src/main/java/safeluck/drive/evaluation/bean/RegisterMessage.java +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterMessage.java @@ -1,11 +1,11 @@ -package safeluck.drive.evaluation.bean; +package safeluck.drive.evaluation.platformMessage; import android.text.TextUtils; import com.anyun.im_lib.util.ByteUtil; -import static com.anyun.im_lib.util.ByteUtil.shortGetBytes; +import safeluck.drive.evaluation.platformMessage.DriveExamProtocol; /** * 娉ㄥ唽娑堟伅 diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterResp.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterResp.java new file mode 100644 index 0000000..9651609 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/RegisterResp.java @@ -0,0 +1,35 @@ +package safeluck.drive.evaluation.platformMessage; + +import com.anyun.exam.lib.MyLog; +import com.anyun.im_lib.util.ByteUtil; + + +import safeluck.drive.evaluation.im.MessageProcessor; + +public class RegisterResp extends ServerProtocol { + private String hexpwd; + private short result; + private int currPos = 0; + + public RegisterResp(byte[] rspBytes) { + super(rspBytes); + } + + @Override + protected void parseMsgBody(byte[] msgbodyData) { + currPos += 2; + result = ByteUtil.getShort(ByteUtil.subArray(msgbodyData,currPos,1)); + if (result == PlatFormConstant.SUCCESS){ + MyLog.i(PlatFormConstant.TAG,"璁惧娉ㄥ唽鎴愬姛"); + AuthMessage authMessage = new AuthMessage((short) 0x101); + + currPos +=1; + hexpwd = ByteUtil.byte2HexStr(ByteUtil.subArray(msgbodyData,currPos,msgBodyLength-3)); + MyLog.i(PlatFormConstant.TAG,"des pwd:"+hexpwd); + authMessage.setHexStrPwd(hexpwd); + MessageProcessor.getInstance().sendMessage(authMessage.toBytes()); + }else{ + MyLog.i(PlatFormConstant.TAG,"璁惧娉ㄥ唽澶辫触"); + } + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerCommonRsp.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerCommonRsp.java new file mode 100644 index 0000000..f902c37 --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerCommonRsp.java @@ -0,0 +1,52 @@ +package safeluck.drive.evaluation.platformMessage; + +import com.anyun.im_lib.util.ByteUtil; + +/** + * 鏈嶅姟绔�氱敤搴旂瓟 + */ +public class ServerCommonRsp extends ServerProtocol { + + /** + * 80 + * 80 01 + * 00 05 + * 00 08 61 85 13 02 12 45 + * 30 AF + * FF + * + * 00 02 + * 01 01 + * 00 + * + * CC + * + * 鍑哄幓澶村熬鏍囪瘑浣�0x7e鍚庣殑 閫氱敤鏈嶅姟鍣ㄥ簲绛旀秷鎭� + */ + private short result; + private int currPos=0; + + private short msgId;//瀵瑰簲瀹㈡埛绔殑娑堟伅ID锛� + + private short msg_serial_num;//瀵瑰簲瀹㈡埛绔粰鐨勬秷鎭祦姘村彿 + /** + * 闄や簡澶村熬鏍囩ず浣�0x7e鐨勬秷鎭� + * @param message + */ + public ServerCommonRsp(byte[] message) { + super(message); + } + + @Override + protected void parseMsgBody(byte[] msgbodyData) { + byte[] msg_serial_numBytes = ByteUtil.subArray(msgbodyData,currPos,2); + currPos += 2; + byte[] msgIdBytes = ByteUtil.subArray(msgbodyData,currPos,2); + currPos += 2; + result = ByteUtil.getShort(ByteUtil.subArray(msgbodyData,currPos,1)); + } + + public short getResult() { + return result; + } +} diff --git a/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerProtocol.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerProtocol.java new file mode 100644 index 0000000..6c5826f --- /dev/null +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/ServerProtocol.java @@ -0,0 +1,51 @@ +package safeluck.drive.evaluation.platformMessage; + +import com.anyun.im_lib.util.ByteUtil; + +public abstract class ServerProtocol { + /**娉ㄥ唽搴旂瓟娑堟伅涓轰緥 + * 80 鐗堟湰鍙� + * 81 00 娑堟伅id + * 00 0B 娑堟伅浣撳睘鎬э紙閲岄潰娑堟伅浣撻暱搴︽湁鐢級 + * 00 00 00 00 00 00 00 00 鐢佃瘽 + * 1E 5E 娴佹按鍙� + * FF 淇濈暀瀛楄妭 + * + * + * 涓嬮潰鏄簲绛斿唴瀹� + * 00 00 + * 00 + * 01 02 03 04 05 06 07 08 + * BD + */ + private short version ;//鐗堟湰鍙� 涓�涓瓧鑺� + protected short msgId;//娑堟伅id 涓や釜瀛楄妭 澶х + protected short msgBodyLength; //娑堟伅浣撻暱搴� + private String phone; // 8涓瓧鑺傜殑string + private short message_serial_num;//娴佹按鍙凤紝涓や釜瀛楄妭 + private byte reserved;//淇濈暀瀛楁 涓�涓瓧鑺� + private int pos = 0; + + public ServerProtocol(byte[] rspBytes) { + + byte[] versionByte = ByteUtil.subArray(rspBytes,pos,1); + this.version = ByteUtil.getShort(versionByte); + pos+=1;//娑堟伅ID寮�濮嬬殑浣嶇疆 + + + pos += 2;// 娑堟伅灞炴�у紑濮嬬殑浣嶇疆 + byte[] msgProperty = ByteUtil.subArray(rspBytes,pos,2); + msgBodyLength |= (msgProperty[1]&0x3f); + pos+=2; //鐢佃瘽寮�濮嬬殑瀛楄妭浣嶇疆 + + pos += 8;// liusshui鍙峰紑濮嬬殑浣嶇疆 + + pos +=2;//淇濈暀瀛楄妭寮�濮嬬殑浣嶇疆 + + pos+=1;//娑堟伅浣撶涓�涓瓧鑺傚紑濮嬬殑浣嶇疆 瀛愮被鍙互鐩存帴鍙杙os浣嶇疆 + + parseMsgBody(ByteUtil.subArray(rspBytes,pos,rspBytes.length-(pos))); + } + + protected abstract void parseMsgBody(byte[] msgbodyData); +} diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/StartExamMessage.java b/app/src/main/java/safeluck/drive/evaluation/platformMessage/StartExamMessage.java similarity index 92% rename from app/src/main/java/safeluck/drive/evaluation/bean/StartExamMessage.java rename to app/src/main/java/safeluck/drive/evaluation/platformMessage/StartExamMessage.java index 47a012a..d08cdef 100644 --- a/app/src/main/java/safeluck/drive/evaluation/bean/StartExamMessage.java +++ b/app/src/main/java/safeluck/drive/evaluation/platformMessage/StartExamMessage.java @@ -1,4 +1,4 @@ -package safeluck.drive.evaluation.bean; +package safeluck.drive.evaluation.platformMessage; /** * 缁欏钩鍙板彂閫佸紑濮嬭�冭瘯娑堟伅 diff --git a/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java b/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java index 9007df3..a50730d 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java +++ b/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java @@ -50,7 +50,7 @@ * 鍙戦�佹秷鎭� * @param msg */ - void sendMsg(String msg); + void sendMsg(byte[] msg); /** * 鍙戦�佹秷鎭� @@ -58,7 +58,7 @@ * @param msg * @param isJoinTimeoutManager 鏄惁鍔犲叆瓒呮椂绠$悊鍣� */ - void sendMsg(String msg, boolean isJoinTimeoutManager); + void sendMsg(byte[] msg, boolean isJoinTimeoutManager); /** * 鑾峰彇閲嶈繛闂撮殧鏃堕暱 diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java index 70cd0f4..9b233a5 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java +++ b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java @@ -311,7 +311,7 @@ } @Override - public void sendMsg(String msg) { + public void sendMsg(byte[] msg) { this.sendMsg(msg,true); } @@ -321,7 +321,7 @@ * @param isJoinTimeoutManager 鏄惁鍔犲叆瓒呮椂绠$悊鍣� */ @Override - public void sendMsg(String msg, boolean isJoinTimeoutManager) { + public void sendMsg(byte[] msg, boolean isJoinTimeoutManager) { if (msg==null ){ return; } @@ -332,7 +332,7 @@ } try { ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(); - byteBuf.writeBytes(msg.getBytes()); + byteBuf.writeBytes(msg); channel.writeAndFlush(byteBuf); } catch (Exception e) { Log.i(TAG, "鍙戦�佹秷鎭け璐ワ紝reason="+e.getMessage()+"\t"+msg); diff --git a/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java index 5761069..d871bef 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java +++ b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java @@ -107,6 +107,7 @@ */ public static byte[] hexStr2Bytes(String src) { + src = src.replace(" ",""); int m=0,n=0; int l=src.length()/2; System.out.println(l); @@ -184,7 +185,7 @@ } /** - * short杞负瀛楄妭鏁扮粍 + * short杞负瀛楄妭鏁扮粍 澶х * @param data * @return 鍖呭惈2涓瓧鑺傜殑瀛楄妭鏁扮粍 */ @@ -192,6 +193,20 @@ byte[] bytes = new byte[2]; bytes[1] = (byte) (data & 0xff); bytes[0] = (byte) ((data & 0xff00) >> 8); + return bytes; + } + + /** + * 澶х int 杞垚 4涓瓧鑺傜殑byte鏁扮粍 + * @param data + * @return + */ + public static byte[] intGetBytes(int data) { + byte[] bytes = new byte[4]; + bytes[3] = (byte) ((data & 0xff)); + bytes[2] = (byte) ((data & 0xff00) >> 8); + bytes[1] = (byte) ((data & 0xff0000) >> 16); + bytes[0] = (byte) ((data & 0xff000000) >> 24); return bytes; } @@ -217,6 +232,11 @@ System.arraycopy(shortGetBytes((short)3),0,messageBody,22,2); System.out.println(byte2HexStr(messageBody)); + + byte[] bytes =new byte[]{(byte) 0x80}; + short aShort= getShort(bytes); + System.out.println(aShort); + } public static byte[] subArray(byte[] srcBytes, int begin, int length) { @@ -225,4 +245,15 @@ Log.i(TAG, "subArray: "+byte2HexStr(bytes)); return bytes; } + + public static short getShort(byte[] bytes) { + short s =0; + if (bytes.length==1){ + s|=(bytes[0]&0xff); + return s; + } + s =(short) ((0xff & bytes[1]) | (0xff00 & (bytes[0] << 8))); + Log.i(TAG, "getShort: "+s); + return s ; + } } -- Gitblit v1.8.0