-
Notifications
You must be signed in to change notification settings - Fork 49
/
server.js
113 lines (93 loc) · 3.06 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
*
* This is the server which runs all Websocket and WebRTC communications for our application.
*
*/
// Set up an express application to run the server
const express = require("express");
const app = express();
// tell our express application to serve the 'public' folder
app.use(express.static("public"));
// tell the server to listen on a given port
const port = process.env.PORT || 8080;
const server = app.listen(port);
console.log("Webserver is running on http://localhost:" + port);
// We will use the socket.io library to manage Websocket connections
const io = require("socket.io")().listen(server);
// add the database application 'nedb'
const Datastore = require("nedb");
// create our database and save it to a local file
const db = new Datastore({ filename: "mydatabase.json", autoload: true });
// We will use this object to store information about active peers
let peers = {};
function main() {
setupSocketServer();
// periodically update all peers with their positions
setInterval(function () {
io.sockets.emit("positions", peers);
}, 100);
}
main();
function setupSocketServer() {
// Set up each socket connection
io.on("connection", (socket) => {
console.log(
"Peer joined with ID",
socket.id,
". There are " + io.engine.clientsCount + " peer(s) connected."
);
// add a new peer indexed by their socket id
peers[socket.id] = {
position: [0, 0.5, 0],
rotation: [0, 0, 0, 1], // stored as XYZW values of Quaternion
};
// send the new peer a list of all other peers
socket.emit("introduction", Object.keys(peers));
// also give the peer all existing peers positions:
socket.emit("userPositions", peers);
// also give them existing data in the database
db.find({}, (err, docs) => {
if (err) return;
for (let i = 0; i < docs.length; i++) {
let doc = docs[i];
socket.emit("data", doc);
}
});
// tell everyone that a new user connected
io.emit("peerConnection", socket.id);
// whenever the peer moves, update their movements in the peers object
socket.on("move", (data) => {
if (peers[socket.id]) {
peers[socket.id].position = data[0];
peers[socket.id].rotation = data[1];
}
});
// setup a generic ping-pong which can be used to share arbitrary info between peers
socket.on("data", (data) => {
// insert data into the database
db.insert(data);
// then send it to all peers
io.sockets.emit("data", data);
});
// Relay simple-peer signals back and forth
socket.on("signal", (to, from, data) => {
if (to in peers) {
io.to(to).emit("signal", to, from, data);
} else {
console.log("Peer not found!");
}
});
// handle disconnections
socket.on("disconnect", () => {
delete peers[socket.id];
io.sockets.emit("peerDisconnection", socket.id);
console.log(
"Peer " +
socket.id +
" diconnected, there are " +
io.engine.clientsCount +
" peer(s) connected."
);
});
});
}