-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
128 lines (107 loc) · 3.79 KB
/
server.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { createServer } from 'http'
import express from 'express'
import { Server } from 'socket.io'
import cors from 'cors'
const allowedOrigins = [process.env.FRONTEND_URL || "http://localhost:3000", "https://co-coding-c6wn.vercel.app"];
const port = process.env.PORT || 3001
const app = express()
const server = createServer(app)
app.use(cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ["GET", "POST"]
}));
const io = new Server(server, {
cors: {
origin: allowedOrigins,
methods: ["GET", "POST"]
}
});
interface Room {
users: Set<string>;
content: string;
editingUser: number | null;
lastEditPosition: number;
}
interface Settings {
switchTrigger: string;
cursorAtEnd: boolean;
}
const rooms = new Map<string, Room>();
io.on('connection', (socket) => {
let currentRoom: string | null = null;
let userId: number | null = null;
socket.on('joinRoom', (roomId: string) => {
if (!rooms.has(roomId)) {
rooms.set(roomId, { users: new Set(), content: '', editingUser: null, lastEditPosition: 0 });
}
const room = rooms.get(roomId)!;
if (room.users.size < 2) {
room.users.add(socket.id);
socket.join(roomId);
currentRoom = roomId;
userId = room.users.size;
socket.emit('userId', userId);
socket.emit('initialContent', room.content);
if (room.users.size === 1) {
room.editingUser = 1;
io.to(roomId).emit('setEditingUser', 1);
} else {
io.to(roomId).emit('setEditingUser', room.editingUser);
}
} else {
socket.emit('roomFull');
}
});
socket.on('contentChange', ({ roomId, content, cursorPosition, settings }: { roomId: string, content: string, cursorPosition: number, settings: Settings }) => {
const room = rooms.get(roomId);
if (room && room.editingUser === userId) {
const oldContent = room.content;
room.content = content;
const switchTrigger = settings.switchTrigger || '\n';
const newContentAfterLastEdit = content.slice(room.lastEditPosition);
if (newContentAfterLastEdit.includes(switchTrigger)) {
room.editingUser = userId === 1 ? 2 : 1;
room.lastEditPosition = content.length;
io.to(roomId).emit('setEditingUser', room.editingUser);
} else if (cursorPosition < room.lastEditPosition) {
room.lastEditPosition = cursorPosition;
}
socket.to(roomId).emit('contentChange', content);
}
});
socket.on('requestEditPermission', (roomId) => {
const room = rooms.get(roomId);
if (room && room.editingUser !== userId) {
room.editingUser = userId;
room.lastEditPosition = room.content.length;
io.to(roomId).emit('setEditingUser', userId);
}
});
socket.on('disconnect', () => {
if (currentRoom) {
const room = rooms.get(currentRoom);
if (room) {
room.users.delete(socket.id);
if (room.users.size === 0) {
rooms.delete(currentRoom);
} else {
room.editingUser = 1;
room.lastEditPosition = 0;
io.to(currentRoom).emit('setEditingUser', 1);
}
}
}
});
});
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
server.listen(port, () => {
console.log(`> Backend ready on http://localhost:${port}`);
});