-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
290 lines (240 loc) · 8.09 KB
/
server.js
File metadata and controls
290 lines (240 loc) · 8.09 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const fs = require('fs');
const path = require("path")
const url = require('url');
const assert = require("assert");
const http = require("http");
const https = require("https");
const express = require("express");
const ws = require("ws");
const { v4: uuidv4 } = require("uuid")
const { Message, PoseData } = require('./public/networkMessages.js');
// const jsonpatch = require("json8-patch");
// const { exit } = require("process");
// const dotenv = require("dotenv").config();
const dotenv = require("dotenv").config();
const merge = require('./public/merge.js');
// this will be true if this server is running on Heroku
const IS_HEROKU = (process.env._ && process.env._.indexOf("heroku") !== -1);
// this will be true if we are running on Heroku (HTTP ONLY), or there's no .env file
const IS_HTTP = IS_HEROKU || (!process.env.PORT_HTTP);
const PORT_HTTP = IS_HTTP ? (process.env.PORT || 3000) : (process.env.PORT_HTTP || 80);
const PORT_HTTPS = process.env.PORT_HTTPS || 443;
const PORT = IS_HTTP ? PORT_HTTP : PORT_HTTPS;
//const PORT_WS = process.env.PORT_WS || 8090; // not used unless you want a second ws port
const PUBLIC_PATH = path.join(__dirname, "public");
// allow cross-domain access (CORS)
const app = express();
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
return next();
});
// promote http to https:
if (!IS_HTTP) {
http.createServer(function(req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(PORT_HTTP);
}
// create the primary server:
const server = IS_HTTP ? http.createServer(app) : https.createServer({
key: fs.readFileSync(process.env.KEY_PATH),
cert: fs.readFileSync(process.env.CERT_PATH)
}, app);
// serve static files from PUBLIC_PATH:
app.use(express.static(PUBLIC_PATH));
// default to index.html if no file given:
app.get("/", function(req, res) {
res.sendFile(path.join(PUBLIC_PATH, "index.html"))
});
// add a websocket server:
const wss = new ws.Server({ server });
// start the server:
server.listen(PORT, function() {
console.log("\nNode.js listening on port " + PORT);
});
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
console.log("GRAHAMS GREAT TEST")
// Audio Server
require('child_process').fork('audioSignalingServer.js');
//if (location.hostname === "localhost" || location.hostname === "127.0.0.1"){
//require('child_process').fork('audioSignalingServer.js');
// }
// else{
// //socket = io('https://agile-basin-71343.herokuapp.com/', { transports : ['websocket'] })
// }
const demoproject = {
threejs: {
geometries: [{ uuid: "geom_cube", type: "BoxGeometry" }],
materials: [{ uuid: "mat_cube", type: "MeshStandardMaterial" }],
object: {
type: "Scene",
children: [
{ type: "Mesh", geometry: "geom_cube", material: "mat_cube", castShadow: true, matrix: [
0.8775825618903728,
0.22984884706593015,
-0.4207354924039482,
0,
0,
0.8775825618903728,
0.47942553860420295,
0,
0.47942553860420295,
-0.4207354924039482,
0.7701511529340699,
0,
0,
1.5,
0,
1
]}
]
}
}
};
const clients = {}
// a set of uniquely-named rooms
// each room would have a list of its occupants
// a client can only be in one room at a time
const rooms = {
}
// Server ID used for automerge operations.
const serverID = newID();
console.log('server ID: ' + serverID);
// get (or create) a room:
function getRoom(name="default") {
if (!rooms[name]) {
rooms[name] = {
name: name,
clients: {},
project: demoproject,
// Automerge state tracking for the shared scene in this room.
merger: new merge.Merger(null, serverID),
syncNeeded: false
}
}
return rooms[name]
}
/**
* Send a message to all users in a room.
* @param {Room} room
* @param {Message} message
*/
function notifyRoom(room, message) {
if (!room) return;
const clientsInRoom = Object.values(room.clients);
message.sendToAll(clientsInRoom);
}
// generate a unique id if needed
// verify id is unused (or generate a new one instead)
// returns 128-bit UUID as a string:
function newID(id="") {
// Removing hyphens so these IDs can double as automerge actor IDs (AM will not accept hyphens)
while (!id || clients[id]) id = uuidv4().replace(/-/g, '');
return id;
}
// Handle incoming connections as a new user joining a room.
wss.on('connection', (socket, req) => {
// Read the path from the connection request and treat it as a room name, sanitizing and standardizing the format.
// Actual room name might differ from this, if it's empty and we need to substitute a "default" instead.
const requestedRoomName = url.parse(req.url).pathname.replace(/\/*$/, "").replace(/\/+/, "/")
const clientID = newID();
let client = {
socket: socket,
room: getRoom(requestedRoomName),
shared: {
// Structure for any rapidly-changing data (poses, etc.)
volatile: {
id: clientID,
poses: [new PoseData()],
},
// Structure for rarely-changing user configuration (display name, colour, etc.)
user: {}
}
}
clients[clientID] = client
// enter this room
client.room.clients[clientID] = client;
client.room.merger.addClient(clientID);
client.trySync = function() {
const payload = client.room.merger.makeSyncMessage(clientID);
if (payload) {
const toSend = `[${payload.toString()}]`;
const message = new Message('sync', toSend);
//console.log('sending: ', toSend, typeof(toSend), 'to: ', clientID);
message.sendWith(socket);
return true;
}
return false;
}
// Convenience function for getting everyone in the room *except* this client.
function getOthersInRoom() {
return Object.values(client.room.clients).filter(c => c.shared.volatile.id != clientID);
}
console.log(`client ${client.shared.volatile.id} connecting to room ${client.room.name}`);
socket.on('message', (data) => {
const msg = Message.fromData(data);
switch(msg.cmd) {
case "pose":
// New pose update (and possibly other rapidly-changing data) from the client.
client.shared.volatile = msg.val;
// Insert our ID into this structure, so we can just batch-send all the volatile
// info together and it already has our ID packed in.
client.shared.volatile.id = clientID;
break;
case "user":
// TODO: Send update to other users about changed info.
client.shared.user = msg.val;
// Tell everyone about the new/updated user.
(new Message("user", {id: clientID, user: msg.val})).sendToAll(getOthersInRoom());
break;
case "sync":
// Handle an automerge synchronization message from the client.
client.room.merger.handleSyncMessage(JSON.parse(msg.val), clientID);
// Flag that we may have new updates to propagate out to the other users in the room.
client.room.syncNeeded = true;
// Check to see if we need to reply back with more synchronization conversation,
// and if so, do so.
client.trySync();
break;
}
});
socket.on('error', (err) => {
console.log(err)
// should we exit?
});
socket.on('close', () => {
// console.log(Object.keys(clients))
delete clients[clientID]
// remove from room
if (client.room) delete client.room.clients[clientID]
// Tell everyone this user left.
notifyRoom(client.room, new Message("exit", clientID));
console.log(`client ${clientID} left room ${client.room.name}`);
});
// Welcome the new user and tell them their unique id.
// TODO: Tell them their spawn position too.
(new Message("handshake", {
id: clientID,
serverID: serverID,
others: getOthersInRoom().map(o=>o.shared)
})).sendWith(socket);
// Share the current 3D scene with the user.
//(new Message("project", client.room.project)).sendWith(socket);
client.trySync();
});
setInterval(function() {
for (let roomid of Object.keys(rooms)) {
const room = rooms[roomid];
const clientlist = Object.values(room.clients);
const message = new Message("others", clientlist.map(o=>o.shared.volatile));
message.sendToAll(clientlist);
if (room.syncNeeded) {
for (let client of clientlist) {
client.trySync();
}
}
room.syncNeeded = false;
}
}, 1000/30);