-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (61 loc) · 2.13 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const fs = require('fs');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const port = 3000;
const filePath = './sharedcode.txt';
// Create HTTP server
const server = http.createServer(app);
// Initialize Socket.io with the server
const io = socketIo(server);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public')); // Serve static files from 'public' directory
// Helper function to read the shared code from the file
const readSharedCode = () => {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (err) {
return ''; // Return empty string if file does not exist
}
};
// API to get the shared code when page loads
app.get('/api/code', (req, res) => {
const code = readSharedCode();
res.json({ code });
});
// API to share code and notify all clients
app.post('/api/code', (req, res) => {
const code = req.body.code;
if (code) {
fs.appendFileSync(filePath, code + '\n'); // Append new code to the file
res.json({ message: 'Code shared successfully!' });
// Notify all connected clients to update their shared code
io.emit('newCode', { code });
} else {
res.status(400).json({ message: 'No code provided.' });
}
});
// API to clear shared code and notify all clients
app.delete('/api/code', (req, res) => {
fs.writeFileSync(filePath, ''); // Clear the file contents
res.json({ message: 'Shared code cleared successfully.' });
// Notify all connected clients to clear the shared code
io.emit('clearCode');
});
// WebSocket connection handling
io.on('connection', (socket) => {
console.log('A user connected');
// Send the current shared code to the new client
const currentCode = readSharedCode();
socket.emit('currentCode', { code: currentCode });
// Handle disconnection
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
// Start server with Socket.io support
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});