-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
91 lines (72 loc) · 3.04 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// server.js
// this is the "hub" where the players' data gets sent to each other.
// You 'probably' won't need to modify this file.
// Here's a little socket.io "quick start" summary:
// to send a message: socket.emit(title,data);
// to deal with a received message: socket.on(title,function(data){ frob(data); })
//========================================================================
const MAX_PLAYERS = 6; // maximum number of players, which is 2 by default.
// you can freely change it to another number here,
// but will need to update the drawing code in public/sketch.js accordingly
// express (https://expressjs.com/) is a simple node.js framework for writing servers
const express = require("express");
const app = express();
let server = app.listen(process.env.PORT || 3000);
// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// socket.io is a simple library for networking
let io = require('socket.io')(server);
let serverData = {}; // everyone's data
let numPlayers = 0; // current number of players
let updateCounter = 0;
console.log("listening...")
// What to do when there's a new player connection:
io.sockets.on('connection', newConnection);
function newConnection(socket){
// Note: "socket" now refers to this particular new player's connection
console.log('new connection: ' + socket.id);
// if there're too many players, reject player's request to join
if (numPlayers >= MAX_PLAYERS){
socket.emit("connection-reject");
return;
}
numPlayers++;
// OK you're in!
socket.emit("connection-approve");
// What to do when client sends us a message entitled 'client-update'
socket.on('client-update',function(data){
// Here the client updates us about itself
// in this simple example, we just need to dump the client's data
// into a big table for sending to everyone later!
serverData[socket.id] = data;
updateCounter++;
})
// Every few milliseconds we send, to this client,
// the data of everybody else's client.
// Note: setInterval(f,t) = runs function f every t milliseconds
let timer = setInterval(function(){
// let others = {};
// for (let k in serverData){
// if (k != socket.id){
// others[k] = serverData[k];
// }
// }
// socket.emit('server-update', others);
socket.emit('server-update', serverData);
}, 100);
// What to do if the client disconnected: let's clean up after them.
socket.on('disconnect', function(){
clearInterval(timer); // cancel the scheduled updates we set up earlier
delete serverData[socket.id];
console.log(socket.id+' disconnected');
numPlayers--;
});
// Egads, we received the "crash-the-server" message!
// Time for us to restart by deliberately causing an error.
socket.on('crash-the-server', function(){
console.log("crashing...")
let notFun = undefined;
notFun(); // calling the undefined notFun() causes a (desired) crash!
});
}