提交 eda75188 编写于 作者: 浅梦2013's avatar 浅梦2013

代码优化。

上级 49a374f5
...@@ -89,10 +89,13 @@ public class MqttWsMsgHandler implements IWsMsgHandler { ...@@ -89,10 +89,13 @@ public class MqttWsMsgHandler implements IWsMsgHandler {
*/ */
@Override @Override
public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext context) { public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext context) {
ByteBuffer buffer = getMqttBody(bytes, context); WriteBuffer wsBody = (WriteBuffer) context.get(MQTT_WS_MSG_BODY_KEY);
ByteBuffer buffer = getMqttBody(wsBody, bytes, context);
if (buffer == null) { if (buffer == null) {
return null; return null;
} }
// 重置 buffer
buffer.rewind();
// 解析 mqtt 消息 // 解析 mqtt 消息
MqttMessage mqttMessage = new MqttDecoder().decode(context, buffer, 0, 0, buffer.remaining()); MqttMessage mqttMessage = new MqttDecoder().decode(context, buffer, 0, 0, buffer.remaining());
if (mqttMessage == null) { if (mqttMessage == null) {
...@@ -185,8 +188,7 @@ public class MqttWsMsgHandler implements IWsMsgHandler { ...@@ -185,8 +188,7 @@ public class MqttWsMsgHandler implements IWsMsgHandler {
* @param context ChannelContext * @param context ChannelContext
* @return ByteBuffer * @return ByteBuffer
*/ */
private static synchronized ByteBuffer getMqttBody(byte[] bytes, ChannelContext context) { private static synchronized ByteBuffer getMqttBody(WriteBuffer wsBody, byte[] bytes, ChannelContext context) {
WriteBuffer wsBody = (WriteBuffer) context.get(MQTT_WS_MSG_BODY_KEY);
wsBody.writeBytes(bytes); wsBody.writeBytes(bytes);
int length = wsBody.size(); int length = wsBody.size();
if (length < 2) { if (length < 2) {
...@@ -199,7 +201,6 @@ public class MqttWsMsgHandler implements IWsMsgHandler { ...@@ -199,7 +201,6 @@ public class MqttWsMsgHandler implements IWsMsgHandler {
} }
// 数据已经读取完毕 // 数据已经读取完毕
wsBody.reset(); wsBody.reset();
buffer.rewind();
return buffer; return buffer;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册