- Added package.json for project metadata and dependencies - Created public/index.html for the front-end interface - Implemented server.js to set up an Express server with Socket.IO for real-time communication
36 lines
832 B
JavaScript
36 lines
832 B
JavaScript
const express = require('express');
|
|
const http = require('http');
|
|
const socketIo = require('socket.io');
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = socketIo(server, {
|
|
cors: {
|
|
origin: "*",
|
|
methods: ["GET", "POST"]
|
|
}
|
|
});
|
|
|
|
app.use(express.static('public'));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(__dirname + '/index.html');
|
|
});
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('New client connected:', socket.id);
|
|
|
|
socket.on('disconnect', () => {
|
|
console.log('Client disconnected:', socket.id);
|
|
});
|
|
|
|
socket.on('message', (data) => {
|
|
console.log('Message received:', data);
|
|
io.emit('message', data);
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
server.listen(PORT, () => {
|
|
console.log(`Server running on port ${PORT}`);
|
|
}); |