// server.js const express = require('express'); const http = require('http'); const WebSocket = require('ws'); const path = require('path'); const app = express(); const port = 7860; // Serve static files (if any) app.use(express.static('public')); // Data structures to store client data const clients = {}; // HTTP server const server = http.createServer(app); // WebSocket server const wss = new WebSocket.Server({ server }); // Handle new WebSocket connections wss.on('connection', (ws) => { const clientId = generateId(); clients[clientId] = { ws: ws, html: '', css: '', url: '' }; console.log(`Client ${clientId} connected`); ws.on('message', (message) => { try { const data = JSON.parse(message); if (data.type === 'sessionInfo') { if(data.sessionId){ clients[clientId].sessionId = data.sessionId; } } if (data.type === 'pageContent') { clients[clientId].html = data.html; clients[clientId].css = data.css; clients[clientId].url = data.url; } else if (data.type === 'userInteraction') { // Handle user interactions if needed console.log(`Interaction from ${clientId}:`, data); } } catch (e) { console.error('Error parsing message:', e); } }); ws.on('close', () => { console.log(`Client ${clientId} disconnected`); delete clients[clientId]; }); }); // Generate a unique client ID function generateId() { return Math.random().toString(36).substr(2, 9); } // Tailwind Play CDN for including Tailwind CSS const tailwindCDN = ``; // Route to list all active clients app.get('/', (req, res) => { // Get the list of active clients const clientKeys = Object.keys(clients); // If no clients are available if (clientKeys.length === 0) { return res.send(` ${tailwindCDN}