sockector.addRequest(requestData);
requestData.put(Constants.FILED_MSG, msg);*/
do{
Request requestData= sockector.getRequestWithFile();
String requestIndex = requestData.getCrrentRequestIndex();
WebSocketMessage messageFrame = null;
if(MyStringUtil.isBlank(requestIndex)){// 没有出现半包的情况
messageFrame = new WebSocketMessage();
requestIndex = requestData.setMessageHeader(messageFrame);
}else{// 出现半包的情况
messageFrame = requestData.<WebSocketMessage>getMessageHeader(requestIndex);
}
if(requestData.readFinish()){
requestData.setByteDatas(new ArrayList<byte[]>(2));
}
if(!messageFrame.isReadFinish()){
messageFrame.parseMessageHeader(buffer);// 读取解析消息头
requestData.setDataSizeLeftLong(messageFrame.getDateLength());// 设置数据长度
}
if(!requestData.readFinish()){// 是否读取完数据
int bufferDataLength = buffer.limit() - buffer.position();
int dataLength = bufferDataLength > requestData.getDataSizeLeftLong() ? requestData.getDataSizeLeftLong().intValue() : bufferDataLength;
byte[] datas = new byte[dataLength];
if(dataLength > 0){
buffer.get(datas);
if(messageFrame.isMask()){// 做加密处理
for (int i = 0; i < dataLength; i++) {
datas[i] ^= messageFrame.getMaskingKey()[(int) (requestData.getDataPosition() % 4)];
requestData.setDataPosition(requestData.getDataPosition() + 1);
}
}else{// 没做加密处理
requestData.setDataPosition(requestData.getDataPosition() + dataLength);
}
requestData.setDataSizeLeftLong(requestData.getDataSizeLeftLong() - dataLength);// 设置剩余数量的数据
requestData.getByteDatas().add(datas);
}
if(requestData.readFinish()){// 消息读取完毕,放入处理队列中
log.info("jason,the msg is : " + requestData.getRequestMessage());
HashMap<String,String> data = MyStringUtil.parseKeyValue(requestData.getRequestMessage());
sockector.addRequest(data);
data.put(WebSocketConstants.REQUEST_INDEX, requestIndex);
requestData.clear();// 清空字节数组
}else{
log.info("jason,the msg is : 78" );
}
}
}while(buffer.limit() > buffer.position());// 处理粘包的情况