import { WebSocketServer } from 'ws';

// Create a WebSocket server
const WS_PORT = 8080;
const wss = new WebSocketServer({ port: WS_PORT });

console.log('WebSocket server is running on ws://localhost:' + String(WS_PORT));

// Connection event handler
wss.on('connection', (ws) => {
  console.log('Client connected');

  // Message event handler
  ws.on('message', (message) => {
    let msg_str = String(message);
    console.log("Received: " + msg_str);
    
    // If this is a text or state message (no Yjs logic) - echo the message back to the client
    if (msg_str.startsWith("TEXT_MESSAGE") | msg_str.startsWith("STATE_MESSAGE")) {
      ws.send(msg_str);
    }
  });

  // Close event handler
  ws.on('close', () => {
    console.log('Client disconnected');
  });
}); 
