Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (40 loc) · 1.45 KB
/
Copy pathindex.js
File metadata and controls
50 lines (40 loc) · 1.45 KB
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
// Server-side code
var express = require('express');
var app = express();
var http = require('http').createServer(app);
const io = require('socket.io')(http);
// Creating connection array
var connections = [];
let number = 1;
// Establishing io connection
io.on("connect", (socket) => {
// Add the joined user as new connection in connections array
connections.push(socket);
// Display joined player status
console.log(`Ninja ${number} has connected`);
number = number + 1;
// Syncing output in real-time among all connections
socket.on("output", (o) => {
socket.broadcast.emit("output", o);
})
// Syncing updated code in real-time among all connections
socket.on("codeChange", (newCode) => {
socket.broadcast.emit("codeChange", newCode);
})
// Syncing joined player logos in real-time among all connections
socket.on('joinuser', (username) => {
socket.broadcast.emit('joinuser', username);
})
// Display disconnected player status
socket.on("disconnect", (reason) => {
console.log(`Ninja ${number} is disconnected`);
connections = connections.filter((con) => con.id !== socket.id);
})
})
// Starting express app using public directory
app.use(express.static("public"));
// Exposing port and displaying server start message
var PORT = process.env.YOUR_PORT || process.env.PORT || 8080;
http.listen(PORT, () => {
console.log("Server started on port " + PORT);
})