Browse Source

增加msgpack对long型数据收发的解析

johnclot69 4 months ago
parent
commit
ae53358f22

+ 9 - 2
incubator-core/src/main/java/com/incubator/core/net/ws/MsgBase.java

@@ -4,7 +4,6 @@ import com.alibaba.fastjson2.JSON;
 import org.msgpack.core.MessageBufferPacker;
 import org.msgpack.core.MessagePack;
 import org.msgpack.core.MessageUnpacker;
-import org.msgpack.core.buffer.MessageBuffer;
 import org.msgpack.value.ValueType;
 
 import java.io.ByteArrayInputStream;
@@ -114,7 +113,13 @@ public abstract class MsgBase {
             case STRING:
                 return unpacker.unpackString();
             case INTEGER:
-                return unpacker.unpackInt();
+                // 根据值范围判断是否为 int 或 long
+                long longValue = unpacker.unpackLong();
+                if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
+                    return (int) longValue; // 转换为 int
+                } else {
+                    return longValue; // 返回 long
+                }
             case FLOAT:
                 return unpacker.unpackFloat();
             case BOOLEAN:
@@ -195,6 +200,8 @@ public abstract class MsgBase {
             packer.packString((String) value);
         } else if (value instanceof Integer) {
             packer.packInt((Integer) value);
+        } else if (value instanceof Long) {
+            packer.packLong((Long) value);
         } else if (value instanceof Float) {
             packer.packFloat((Float) value);
         } else if (value instanceof Boolean) {