silphius/app/net/packet.js

27 lines
519 B
JavaScript
Raw Normal View History

2024-06-10 22:42:30 -05:00
import {Encoder, Decoder} from '@msgpack/msgpack';
const decoder = new Decoder();
const encoder = new Encoder();
export default class Packet {
static decode(view) {
return decoder.decode(new DataView(view.buffer, view.byteOffset + 2, view.byteLength - 2));
}
static encode(payload) {
encoder.pos = 2;
encoder.doEncode(payload)
return new DataView(encoder.bytes.buffer, 0, encoder.pos);
}
static pack(payload) {
return payload;
}
static unpack(packed) {
return packed;
}
}