Send chunks to players connected to server
This commit is contained in:
+10
-4
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user