-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (51 loc) · 1.81 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
const express = require('express');
const http = require('http');
const path = require('path');
const { Server } = require('socket.io');
const ACTIONS = require('./src/Actions');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Serve the real-time code editor application
app.use(express.static('build'));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
// Serve the chat application
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Socket.IO connection for both applications
io.on('connection', (socket) => {
console.log('socket connected', socket.id);
// Handle real-time code editor functionality
socket.on(ACTIONS.JOIN, ({ roomId, username }) => {
socket.join(roomId);
socket.to(roomId).emit(ACTIONS.JOINED, {
clients: getClientsInRoom(roomId),
username,
socketId: socket.id,
});
});
socket.on(ACTIONS.CODE_CHANGE, ({ roomId, code }) => {
socket.to(roomId).emit(ACTIONS.SYNC_CODE, { socketId: socket.id, code });
});
socket.on(ACTIONS.CODE_CHANGE, ({ roomId, code }) => {
socket.to(roomId).emit(ACTIONS.CODE_CHANGE, { code });
});
socket.on('disconnecting', () => {
// Implement logic to handle disconnection
});
function getClientsInRoom(roomId) {
const clients = io.sockets.adapter.rooms.get(roomId);
return [...(clients ? clients : [])];
}
// Handle chat application functionality
socket.on('message', (msg) => {
io.emit('message', msg);
});
});
// Start the server
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`));