var decoder = new TextDecoder("utf-8");
var port = 8089;
var ws = require("ws");
var wss = new ws.WebSocketServer({ port: port });
console.log("WebSocket server listening on ws://localhost:" + port + "/");
wss.on("connection", function (ws) {
  console.log("Client connected!");
  ws.on("message", function (message, isBinary) {
    if (!isBinary) {
      var text = decoder.decode(new Uint8Array(message).buffer);
      console.log("received TEXT: " + text.length + " characters:");
      console.log('  "' + text + '"');
    } else {
      const camera = new Camera3D({}, new Uint8Array(message));
      console.log(camera.position.x, camera.position.y, camera.position.z);
      console.log(camera.target.x, camera.target.y, camera.target.z);
      console.log(camera.up.x, camera.up.y, camera.up.z);
      console.log(camera.fovy);
      console.log(camera.projection);
      console.log(camera.bytes);

      ws.send(camera.bytes, { binary: true }); // Echo back the received message
    }
  });
});
