refactor(core): 使用fflate替换msgpack以兼容小游戏环境

This commit is contained in:
YHH
2025-10-28 14:08:34 +08:00
parent 4e81fc7eba
commit 683203919f
7 changed files with 59 additions and 33 deletions

View File

@@ -0,0 +1,25 @@
import { strToU8, strFromU8, zlibSync, unzlibSync } from 'fflate';
/**
* 二进制序列化器
* 使用zlib压缩JSON数据
*/
export class BinarySerializer {
/**
* 将对象编码为压缩的二进制数据
*/
public static encode(value: any): Uint8Array {
const jsonString = JSON.stringify(value);
const utf8Bytes = strToU8(jsonString);
return zlibSync(utf8Bytes);
}
/**
* 将压缩的二进制数据解码为对象
*/
public static decode(bytes: Uint8Array): any {
const decompressed = unzlibSync(bytes);
const jsonString = strFromU8(decompressed);
return JSON.parse(jsonString);
}
}