const socket = new WebSocket("ws://localhost:4000");
let userName = "";

export function sendGhostPosition(x, y, z) {
    const data = {
        type: "ghostPosition",
        name: userName,
        x: x,
        y: y,
        z: z,
        timestamp: Date.now()
    };
    socket.send(JSON.stringify(data));
}

export function sendUserName(name) {
    userName = name;
    const data = {
        type: "userName",
        name: name
    };
    socket.send(JSON.stringify(data));
}

socket.onopen = () => {
    console.log("接続成功！");
};

socket.onerror = (error) => {
    console.error("WebSocketエラー:", error);
};

socket.onmessage = (event) => {
    const data = JSON.parse(event.data);
    if (data.type === "connectionsBroadcast") {
        console.log("接続人数:", data.connections);
    }
};