Skip to content

Commit f4a4bde

Browse files
author
liyoumin
committed
feat: add the pako to ungzip
1 parent 5721201 commit f4a4bde

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@types/js-yaml": "^4.0.5",
3939
"@types/lodash": "^4.14.186",
4040
"@types/node": "^18.11.7",
41+
"@types/pako": "^2.0.0",
4142
"@types/react": "^18.0.24",
4243
"@types/react-dom": "^18.0.8",
4344
"@types/three": "^0.144.0",
@@ -118,6 +119,7 @@
118119
"lodash": "^4.17.21",
119120
"mpegts.js": "^1.7.2",
120121
"mqtt": "^4.3.7",
122+
"pako": "^2.1.0",
121123
"query-string": "^7.1.1",
122124
"rc-menu": "^9.7.1",
123125
"rc-resize-observer": "^1.2.0",

src/components/CloudPoint/index.tsx

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as THREE from 'three';
33
import './index.module.less';
44
import React from 'react';
55
import { OrbitControls } from './OrbitControls';
6-
6+
import pako from 'pako';
77
export interface CloudPointProps {
88
width: number;
99
height: number;
@@ -40,13 +40,40 @@ const CloudPoint: React.FC<CloudPointProps> = (props: CloudPointProps) => {
4040
const cloud = new THREE.Points(geometry, materials);
4141
scene.add(cloud);
4242

43+
function decompressData(compressedData) {
44+
const uint8Array = new Uint8Array(compressedData);
45+
const decompressedData = pako.inflate(uint8Array, { to: 'string' });
46+
return decompressedData;
47+
}
48+
49+
function blobToUint8Array(blob) {
50+
return new Promise((resolve, reject) => {
51+
const reader = new FileReader();
52+
53+
reader.onloadend = () => {
54+
if (reader.readyState === FileReader.DONE) {
55+
const uint8Array = new Uint8Array(reader.result);
56+
resolve(uint8Array);
57+
} else {
58+
reject(new Error('Failed to read Blob as Uint8Array.'));
59+
}
60+
};
61+
62+
reader.onerror = () => {
63+
reject(new Error('Failed to read Blob as Uint8Array.'));
64+
};
65+
66+
reader.readAsArrayBuffer(blob);
67+
});
68+
}
69+
4370
// 更新云点数据
4471
const updatePoint = useCallback(
45-
(pointJson: string) => {
72+
(points: number[]) => {
4673
const point = [];
47-
const pointsArr = JSON.parse(pointJson);
48-
for (let i = 0; i < pointsArr.length; i += 3) {
49-
point.push(new THREE.Vector3(pointsArr[i], pointsArr[i + 1], pointsArr[i + 2]));
74+
75+
for (let i = 0; i < points.length; i += 3) {
76+
point.push(new THREE.Vector3(points[i], points[i + 1], points[i + 2]));
5077
}
5178
geometry.setFromPoints(point);
5279
},
@@ -69,7 +96,15 @@ const CloudPoint: React.FC<CloudPointProps> = (props: CloudPointProps) => {
6996
}
7097
};
7198
ws.current.onmessage = e => {
72-
updatePoint(e.data);
99+
blobToUint8Array(e.data)
100+
.then(compressedDataUint8Array => {
101+
const decompressedData = decompressData(compressedDataUint8Array);
102+
const points = JSON.parse(decompressedData);
103+
updatePoint(points);
104+
})
105+
.catch(error => {
106+
console.error(error);
107+
});
73108
};
74109
ws.current.onclose = () => {
75110
clearInterval(timerRef.current);

0 commit comments

Comments
 (0)