Skip to content

Commit d9a2319

Browse files
authored
Add back extension codes for Uint8Array and Int32Array (#172)
These were accidentally lost in the switch to msgpack-javascript.
1 parent 9292910 commit d9a2319

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ where `dom_element` is the `div` in which the viewer should live. The primary in
455455
<dd>
456456
Set up a web socket connection to a server at the given URL. The viewer will listen for messages on the socket as binary MsgPack blobs. Each message will be decoded using <code>msgpack.decode()</code> from <a href="https://github.com/msgpack/msgpack-javascript">msgpack-javascript</a> and the resulting object will be passed directly to <code>Viewer.handle_command()</code> as documented above.
457457
<p>
458-
Note that we do support the MsgPack extension types listed in <a href="https://github.com/msgpack/msgpack-javascript#extension-types">msgpack-javascript#extension-types</a>, with additional support for the <code>Float32Array</code> type which is particularly useful for efficiently sending point data and for <code>Uint32Array</code>.
458+
Note that we do support the MsgPack extension types listed in <a href="https://github.com/msgpack/msgpack-javascript#extension-types">msgpack-javascript#extension-types</a>, with additional support for the <code>Float32Array</code> type which is particularly useful for efficiently sending point data and for <code>Uint32Array</code>, <code>Uint8Array</code>, and <code>Int32Array</code>.
459459
</dd>
460460
</dl>
461461

dist/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,50 @@ import { XRButton } from 'three/examples/jsm/webxr/XRButton.js';
1313
import { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory';
1414
require('ccapture.js');
1515

16-
// We must implement extension types 0x16 and 0x17. The trick to
17-
// decoding them is they must be converted from littleEndian.
16+
// We implement several MessagePack extension types for arrays, inspired by the
17+
// conventions for msgpack-lite:
18+
// https://github.com/kawanet/msgpack-lite/tree/master#extension-types
19+
//
20+
// Specifically, we support:
21+
// - 0x12 Uint8Array
22+
// - 0x15 Int32Array
23+
// - 0x16 Uint32Array
24+
// - 0x17 Float32Array
25+
//
26+
// The trick to decoding them is they must be converted from littleEndian.
1827
const extensionCodec = new msgpack.ExtensionCodec();
28+
// Uint8Array
29+
extensionCodec.register({
30+
type: 0x12,
31+
encode: (obj) => {
32+
console.error("Uint8Array encode not implemented")
33+
return null;
34+
},
35+
decode: (data) => {
36+
const to_return = new Uint8Array(data.byteLength);
37+
let dataview = new DataView(data.buffer, data.byteOffset, data.byteLength);
38+
for (let i = 0; i < to_return.length; i++) {
39+
to_return[i] = dataview.getUint8(i);
40+
}
41+
return to_return
42+
},
43+
});
44+
// Int32Array
45+
extensionCodec.register({
46+
type: 0x15,
47+
encode: (obj) => {
48+
console.error("Int32Array encode not implemented")
49+
return null;
50+
},
51+
decode: (data) => {
52+
const to_return = new Int32Array(data.byteLength / 4);
53+
let dataview = new DataView(data.buffer, data.byteOffset, data.byteLength);
54+
for (let i = 0; i < to_return.length; i++) {
55+
to_return[i] = dataview.getInt32(i * 4, true); // true b.c. littleEndian
56+
}
57+
return to_return
58+
},
59+
});
1960
// Uint32Array
2061
extensionCodec.register({
2162
type: 0x16,
@@ -32,7 +73,6 @@ extensionCodec.register({
3273
return to_return
3374
},
3475
});
35-
+
3676
// Float32Array
3777
extensionCodec.register({
3878
type: 0x17,

0 commit comments

Comments
 (0)