这个是OkSocket的源码地址:https://github.com/xuuhaoo/OkSocket
这个框架必须要固定包头,我们在实际的使用中一般都是固定包头的,但是为了方便调试或者没有固定包头的,我们需要修改一下源码;
把源码中的这个接收消息类'ReaderImpl'修改一下:
public class ReaderImpl extends AbsReader {
// private ByteBuffer mRemainingBuf;
//
// @Override
// public void read() throws RuntimeException {
// OriginalData originalData = new OriginalData();
// IReaderProtocol headerProtocol = mOkOptions.getReaderProtocol();
// int headerLength = headerProtocol.getHeaderLength();
// ByteBuffer headBuf = ByteBuffer.allocate(headerLength);
// headBuf.order(mOkOptions.getReadByteOrder());
// try {
// if (mRemainingBuf != null) {
// mRemainingBuf.flip();
// int length = Math.min(mRemainingBuf.remaining(), headerLength);
// headBuf.put(mRemainingBuf.array(), 0, length);
// if (length < headerLength) {
// //there are no data left
// mRemainingBuf = null;
// readHeaderFromChannel(headBuf, headerLength - length);
// } else {
// mRemainingBuf.position(headerLength);
// }
// } else {
// readHeaderFromChannel(headBuf, headBuf.capacity());
// }
// originalData.setHeadBytes(headBuf.array());
// if (SLog.isDebug()) {
// SLog.i("read head: " + BytesUtils.toHexStringForLog(headBuf.array()));
// }
// int bodyLength = headerProtocol.getBodyLength(originalData.getHeadBytes(), mOkOptions.getReadByteOrder());
// if (SLog.isDebug()) {
// SLog.i("need read body length: " + bodyLength);
// }
// if (bodyLength > 0) {
// if (bodyLength > mOkOptions.getMaxReadDataMB() * 1024 * 1024) {
// throw new ReadException("Need to follow the transmission protocol.\r\n" +
// "Please check the client/server code.\r\n" +
// "According to the packet header data in the transport protocol, the package length is " + bodyLength + " Bytes.\r\n" +
// "You need check your <ReaderProtocol> definition");
// }
// ByteBuffer byteBuffer = ByteBuffer.allocate(bodyLength);
// byteBuffer.order(mOkOptions.getReadByteOrder());
// if (mRemainingBuf != null) {
// int bodyStartPosition = mRemainingBuf.position();
// int length = Math.min(mRemainingBuf.remaining(), bodyLength);
// byteBuffer.put(mRemainingBuf.array(), bodyStartPosition, length);
// mRemainingBuf.position(bodyStartPosition + length);
// if (length == bodyLength) {
// if (mRemainingBuf.remaining() > 0) {//there are data left
// ByteBuffer temp = ByteBuffer.allocate(mRemainingBuf.remaining());
// temp.order(mOkOptions.getReadByteOrder());
// temp.put(mRemainingBuf.array(), mRemainingBuf.position(), mRemainingBuf.remaining());
// mRemainingBuf = temp;
// } else {//there are no data left
// mRemainingBuf = null;
// }
// //cause this time data from remaining buffer not from channel.
// originalData.setBodyBytes(byteBuffer.array());
// mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
// return;
// } else {//there are no data left in buffer and some data pieces in channel
// mRemainingBuf = null;
// }
// }
// readBodyFromChannel(byteBuffer);
// originalData.setBodyBytes(byteBuffer.array());
// } else if (bodyLength == 0) {
// originalData.setBodyBytes(new byte[0]);
// if (mRemainingBuf != null) {
// //the body is empty so header remaining buf need set null
// if (mRemainingBuf.hasRemaining()) {
// ByteBuffer temp = ByteBuffer.allocate(mRemainingBuf.remaining());
// temp.order(mOkOptions.getReadByteOrder());
// temp.put(mRemainingBuf.array(), mRemainingBuf.position(), mRemainingBuf.remaining());
// mRemainingBuf = temp;
// } else {
// mRemainingBuf = null;
// }
// }
// } else if (bodyLength < 0) {
// throw new ReadException(
// "read body is wrong,this socket input stream is end of file read " + bodyLength + " ,that mean this socket is disconnected by server");
// }
// mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
// } catch (Exception e) {
// ReadException readException = new ReadException(e);
// throw readException;
// }
// }
//
// private void readHeaderFromChannel(ByteBuffer headBuf, int readLength) throws IOException {
// for (int i = 0; i < readLength; i++) {
// byte[] bytes = new byte[1];
// int value = mInputStream.read(bytes);
// if (value == -1) {
// throw new ReadException(
// "read head is wrong, this socket input stream is end of file read " + value + " ,that mean this socket is disconnected by server");
// }
// headBuf.put(bytes);
// }
// }
//
//
// private void readBodyFromChannel(ByteBuffer byteBuffer) throws IOException {
// while (byteBuffer.hasRemaining()) {
// try {
// byte[] bufArray = new byte[mOkOptions.getReadPackageBytes()];
// int len = mInputStream.read(bufArray);
// if (len == -1) {
// break;
// }
// int remaining = byteBuffer.remaining();
// if (len > remaining) {
// byteBuffer.put(bufArray, 0, remaining);
// mRemainingBuf = ByteBuffer.allocate(len - remaining);
// mRemainingBuf.order(mOkOptions.getReadByteOrder());
// mRemainingBuf.put(bufArray, remaining, len - remaining);
// } else {
// byteBuffer.put(bufArray, 0, len);
// }
// } catch (Exception e) {
// throw e;
// }
// }
// if (SLog.isDebug()) {
// SLog.i("read total bytes: " + BytesUtils.toHexStringForLog(byteBuffer.array()));
// SLog.i("read total length:" + (byteBuffer.capacity() - byteBuffer.remaining()));
// }
// }
//接收byte数组
@Override
public void read() throws RuntimeException {
OriginalData originalData = new OriginalData();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while (true) {
byte[] buff = new byte[1024 * 5];
int len = mInputStream.read(buff);
bos.write(buff, 0, len);
originalData.setBodyBytes(bos.toByteArray());
mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
bos.reset();
}
} catch (Exception e) {
ReadException readException = new ReadException(e);
throw readException;
}
}
//接收byte数组
// @Override
// public void read() throws RuntimeException {
// OriginalData originalData = new OriginalData();
// try {
// while (true) {
// byte[] buff = new byte[1024 * 5];
// int len = mInputStream.read(buff);
// byte[] data = Arrays.copyOfRange(buff, 0, len);
// originalData.setBodyBytes(data);
// mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
// }
// } catch (Exception e) {
// ReadException readException = new ReadException(e);
// throw readException;
// }
// }
//接收String
// @Override
// public void read() throws RuntimeException {
// OriginalData originalData = new OriginalData();
// try {
// String response = "";
// while (true) {
// byte[] b = new byte[1024];
// int len = mInputStream.read(b);
// response = new String(b, 0, len);
// originalData.setBodyBytes(response.getBytes());
// mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
// }
// }catch (IOException e){
// e.printStackTrace();
// }
// }
}
代码中有注释,可以选择接收String或Byte数组;需要把原有的read()方法注释;现把数据全部都读到了BobyBytes中,获取数据就 originalData.getBodyBytes() 就行了;
附:本人是项目刚开始就使用的OkSocket作为通讯框架,但后来实际调试中频繁出错,原因是包头转义后长度就不固定了,包体也是会转义的,导致经常接收不到完整的消息;但是现有通讯的心跳、重连机制、消息接收以及各种回调都使用的OkSocket,才被迫修改源码;
如果OkSocket框架规则(包头定长 ,根据包头信息确定包体的长度)不符合你现在的通讯协议,就比如包头有可能被转义 转义后的包头就是不固定长度的了。还是不要使用此框架的好,毕竟改了框架的源码;可以看一下这个:Android Socket长连接的使用和封装