-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (99 loc) · 3 KB
/
server.js
File metadata and controls
100 lines (99 loc) · 3 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
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
const express = require('express');
const app = new express();
const fs = require('fs');
// Certificate
const privateKey = fs.readFileSync('certs/private.key', 'utf8');
const certificate = fs.readFileSync('certs/certificate.crt', 'utf8');
const ca = fs.readFileSync('certs/ca_bundle.crt', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
//HTTPs server
const https = require('https').createServer(credentials, app);
//Socket.io
const io = require('socket.io')(https);
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var users = 0;
var cubes = {};
var ips = []
//Express
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
//Serving files
app.use(express.static(__dirname, { dotfiles: 'allow' } ));
app.use(express.static(".well-known"))
app.use(express.static('scripts'));
app.use(express.static('assets'));
// Socket.IO
io.emit("announcement", "The game has been updated, please reload the page.")
io.on('connection', (socket) => {
var verified = false;
const ip = socket.request.connection.remoteAddress.substr(7, socket.request.connection.remoteAddress.length);
// Prevent same ip from connecting multiple times
if (ips.indexOf(ip) != -1 && ip != "127.0.0.1") {
console.log(`${yellow}${ip} tried to connect more than once.${reset}`);
socket.disconnect();
} else {
verified = true;
ips.push(ip);
console.log(`${yellow}${ip} joined. ID: ${socket.id}`);
}
if (verified) {
// Update user count
users++;
cubes[socket.id] = {};
// Event handling
socket.on('state', (data) => {
cubes[socket.id] = data;
socket.broadcast.emit("update", data, socket.id)
});
socket.on('damage', (data, amount) => {
io.to(data).emit("damage", amount)
});
socket.on('players', () => {
socket.emit("players", users);
});
socket.on("ping", () => {
socket.emit("pong");
})
// Handle disconnections
socket.on("disconnect", () => {
console.log(`${yellow}${ip} left. ID: ${socket.id}${reset}`)
io.emit("disconnect", socket.id)
delete cubes[socket.id];
ips.splice(ips.indexOf(ip), 1);
users--;
})
}
});
//CLI
const black = "\u001b[30m"
const red = "\u001b[31m"
const green = "\u001b[32m"
const yellow = "\u001b[33m"
const blue = "\u001b[34m"
const magenta = "\u001b[35m"
const cyan = "\u001b[36m"
const white = "\u001b[37m"
const reset =" \u001b[0m"
// Host-only: Commands
rl.on('line', function (input) {
if (input == "announcement") {
rl.question(`${red}Enter message:${reset}`, (input) => {
io.emit("announcement", input);
})
} else if (input == "ips") {
console.log(ips)
}
});
//HTTPs server
https.listen(443, () => {
console.log(`${green}HTTPs server started. Listening on :443${reset}`);
});