-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
67 lines (51 loc) · 1.99 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
64
65
66
67
// ************************************************
// Teugg Soccer Physics
// Rip-off of the once popular Soccer Physics game
// Horribly coded by : Kevin Le Teugg, 2020
// File : server.js
// Description : Node.js server
// ************************************************
var express = require('express'); // Equivalent to an "import" command. Imports the express module.
var app = express(); // Triggering the express() function to create an app that is stored in the "app" variable.
var server = app.listen(3000); // Server listens on port 3000 (localhost)
app.use(express.static('public')); // Telling the server where to look for delivering the hosted (static) files.
console.log("Test Node.js");
var socket = require('socket.io');
var io = socket(server);
var allClients = [];
io.sockets.on('connection', newConnection);
function newConnection(socket) {
console.log("Connection ID : " + socket.id + " NEW CONNECTION");
// Get lobby message from game_menus.js
socket.on('createLobbyMessage', handleLobbyName);
}
io.sockets.on('connection', function(socket) {
allClients.push(socket);
socket.on('disconnect', function() {
console.log("Connection ID : " + socket.id + " DISCONNECTED");
var i = allClients.indexOf(socket);
allClients.splice(i, 1);
});
});
//socket.on('createLobbyMessage', handleLobbyName);
function handleLobbyName(lobby) {
console.log(lobby);
}
// Code from https://github.com/LukeGarrigan/p5-multiplayer-game-starter
/*let players = [];
setInterval(updateGame, 16);
io.sockets.on("connection", socket => {
console.log(`New connection ${socket.id}`);
players.push(new Player(socket.id));
socket.on("disconnect", () => {
io.sockets.emit("disconnect", socket.id);
players = players.filter(player => player.id !== socket.id);
});
});
io.sockets.on("disconnect", socket => {
io.sockets.emit("disconnect", socket.id);
players = players.filter(player => player.id !== socket.id);
});
function updateGame() {
io.sockets.emit("heartbeat", players);
}*/