Send chunks to players connected to server

This commit is contained in:
2026-09-28 11:14:40 -03:00
parent 677ad8115a
commit ded9a7e5d0
26 changed files with 920 additions and 357 deletions
+10 -4
View File
@@ -76,8 +76,14 @@ export class ServerSocket {
constructor(socket: WebSocket) {
this.socket = socket;
socket.binaryType = "arraybuffer";
socket.addEventListener("message", (event) => {
this.messages.push(JSON.parse(event.data));
// the only binary messages are chunks, decoded by the packet listener so it can time them
this.messages.push(
typeof event.data === "string"
? JSON.parse(event.data)
: { type: "level_chunk", data: new Uint8Array(event.data) },
);
this.#wake();
});
socket.addEventListener("close", () => {
@@ -151,9 +157,9 @@ export async function connect(
return { socket: server, welcome: answer };
}
// tell the server everything's loaded and wait to be let in
export async function join(socket: ServerSocket): Promise<Join> {
socket.send({ type: "ready" });
// tell the server everything's loaded and wait to be let in. view_distance is in chunks
export async function join(socket: ServerSocket, view_distance: number): Promise<Join> {
socket.send({ type: "ready", view_distance });
const answer = await socket.next("join", "rejected");
if (answer.type === "rejected") {
socket.close();