feat: initial commit with Express server and Socket.IO integration
- 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
This commit is contained in:
36
server.js
Normal file
36
server.js
Normal file
@@ -0,0 +1,36 @@
|
||||
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}`);
|
||||
});
|
||||
Reference in New Issue
Block a user