-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 2.01 KB
/
index.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
const express = require("express");
const http = require("http");
const path = require("path");
const socketio = require("socket.io");
const logF = require("log-to-file");
const app = express();
const server = http.createServer(app); // io requires raw http
const io = socketio(server); // Setup Socket.io's server
// Logger Function
function log(data) {
console.log(data);
// logF(data);
}
app.use(express.static("public"));
// Actual project code
const rooms = {};
io.on("connection", socket => {
console.log("New Connection!");
socket.on("user-joined", data => {
socket.username = data.username;
socket.room = data.room;
if (rooms[data.room]) {
rooms[data.room] = [...rooms[data.room], socket.username];
} else {
rooms[data.room] = [socket.username];
}
socket.join(data.room);
socket.to(data.room).emit("updated-user-data", rooms[socket.room]);
socket.emit("remote-user-joined");
console.log(rooms[socket.room]);
});
socket.on("request-user-data", () => {
console.log("request incoming");
socket.emit("updated-user-data", rooms[socket.room]);
});
socket.on("sync-all", values => {
console.log("emit remote sync");
socket.to(socket.room).emit("remote-sync", values);
});
socket.on("editor-model-content-change", data => {
socket.to(socket.room).emit("remote-content-change", data);
});
socket.on("run-code", data => {
console.log("Run Emitted");
socket.to(socket.room).emit("remote-run-code");
});
socket.on("chat-message", data => {
socket.to(socket.room).emit("remote-chat-message", data);
});
socket.on("disconnect", () => {
console.log(`User "${socket.username}" disconnected`, rooms[socket.room]);
const room = rooms[socket.room] || [];
const index = room.indexOf(socket.username);
delete room[index];
socket.to(socket.room).emit("updated-user-data", rooms[socket.room]);
});
});
// Start server
const port = process.env.PORT || 3000;
server.listen(port, () => log(`Server is running on *:${port}`));