-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
465 lines (349 loc) · 9.79 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* @author pschroen / https://ufo.ai/
*
* Remix of https://glitch.com/edit/#!/multiuser-blocks
*/
import express from 'express';
import enableWs from 'express-ws';
const interval = 4000; // 4 second heartbeat
const app = express();
const expressWs = enableWs(app);
expressWs.getWss('/');
app.use(express.static('public'));
//
import { ObjectPool } from '@alienkitty/space.js/three';
import { numPointers } from './src/config/Config.js';
const mousePool = new ObjectPool();
for (let i = 0; i < numPointers; i++) {
mousePool.put(i);
}
//
const clients = [];
const room = new Array(255);
function getRemoteAddress(request) {
return (request.headers['x-forwarded-for'] || request.connection.remoteAddress).split(',')[0].trim();
}
function getRemoteAddresses() {
return clients.map(ws => ws._remoteAddress);
}
function getUsers() {
if (!clients.length) {
return;
}
const length = clients.length;
const byteLength = 1 + 6 + 4 + 2; // mouse + color + remoteAddress + latency
const data = Buffer.allocUnsafe(1 + byteLength * length); // event + size * users
data.writeUInt8(0, 0);
let index = 1;
for (let i = 0; i < length; i++) {
const client = clients[i];
data.writeUInt8(client._mouse === null ? numPointers : client._mouse, index);
const buf = Buffer.from(client._color, 'utf8');
for (let j = 0; j < 6; j++) {
data.writeUInt8(buf[j], index + 1 + j);
}
data.writeUInt32BE(ip2long(client._remoteAddress), index + 7);
data.writeUInt16BE(client._latency, index + 11);
index += byteLength;
}
// console.log('USERS:', data);
return data;
}
function add(ws, request) {
clients.push(ws);
for (let i = 0, l = room.length; i < l; i++) {
if (room[i] === undefined) {
const remoteAddresses = getRemoteAddresses();
let count = 1;
let remoteAddress = getRemoteAddress(request);
while (remoteAddresses.includes(remoteAddress)) {
count++;
remoteAddress = `${getRemoteAddress(request)} (${count})`;
}
ws._id = i;
ws._idle = Date.now();
ws._mouse = request.query.observer !== undefined ? null : mousePool.get();
ws._isMove = false;
ws._isDown = false;
ws._color = '';
ws._remoteAddress = remoteAddress;
ws._latency;
room[i] = ws;
console.log('REMOTE:', ws._remoteAddress, request.headers['user-agent']);
return;
}
}
}
function remove(ws) {
let index = clients.indexOf(ws);
if (~index) {
clients.splice(index, 1);
}
index = room.indexOf(ws);
if (~index) {
room[index] = undefined;
}
if (ws._mouse !== null) {
// Reset after fade out
setTimeout(() => {
resetMouse(ws._mouse);
mousePool.put(ws._mouse);
}, interval + 1000);
}
}
function broadcast(ws, data) {
for (let i = 0, l = clients.length; i < l; i++) {
const client = clients[i];
if (client !== ws && client.readyState === client.OPEN) {
client.send(data);
}
}
}
function idle() {
const idleTime = Date.now() - 1800000; // 30 * 60 * 1000
for (let i = 0, l = clients.length; i < l; i++) {
const client = clients[i];
if (client._idle === 0) {
client._idle = Date.now();
} else if (client._idle < idleTime) {
client.terminate();
console.log('IDLE:', client._id);
}
}
}
function users(ws) {
broadcast(ws, getUsers());
}
app.ws('/', (ws, request) => {
add(ws, request);
console.log('USERS:', clients.length);
if (timeout === null) {
startTime = 0;
timeout = setTimeout(onUpdate, 0);
console.log('Started physics engine');
}
ws.on('close', () => {
remove(ws);
users(ws);
console.log('USERS:', clients.length);
if (!clients.length) {
clearTimeout(timeout);
timeout = null;
console.log('Stopped physics engine');
}
});
ws.on('message', data => {
ws._idle = 0;
switch (data.readUInt8(0)) {
case 1:
// console.log('HEARTBEAT:', data);
ws._latency = Math.min(65535, Date.now() - Number(data.readBigUInt64BE(2))); // Clamp to 65535
break;
case 4: {
if (ws._mouse !== null) {
// console.log('COLOR:', data);
ws._color = Buffer.from(data.subarray(2), 'utf-8').toString();
users(ws);
}
break;
}
case 5: {
if (ws._mouse !== null) {
const mouse = `mouse_${ws._mouse}`;
const position = [data.readFloatBE(3), data.readFloatBE(7), data.readFloatBE(11)];
// console.log('MOTION:', data, data.readUInt8(2), position);
physics.setPosition(mouse, position);
// First input
if (!ws._isMove) {
physics.wakeUp(mouse);
const sphere = `sphere_${ws._mouse}`;
physics.setPosition(sphere, position);
physics.wakeUp(sphere);
ws._isMove = true;
}
ws._isDown = !!data.readUInt8(2);
}
break;
}
}
// console.log('MESSAGE:', data);
});
const heartbeat = () => {
if (ws.readyState === ws.OPEN) {
const data = Buffer.allocUnsafe(10);
data.writeUInt8(1, 0);
data.writeUInt8(ws._mouse === null ? numPointers : ws._mouse, 1);
data.writeBigUInt64BE(BigInt(Date.now()), 2);
ws.send(data);
setTimeout(heartbeat, interval);
}
};
heartbeat();
users();
});
setInterval(() => {
idle();
users();
}, interval);
//
const listener = app.listen(process.env.PORT, () => {
console.log(`Listening on port ${listener.address().port}`);
});
// https://stackoverflow.com/questions/1908492/unsigned-integer-in-javascript/7414641#7414641
function ip2long(ip) {
let ipl = 0;
ip.split('.').forEach(octet => {
ipl <<= 8;
ipl += parseInt(octet, 10);
});
return ipl >>> 0;
}
//
import { Group, MathUtils, Vector3 } from 'three';
import { headsTails } from '@alienkitty/space.js/three';
import { OimoPhysicsBuffer } from '@alienkitty/alien.js/three/oimophysics';
const center = new Vector3(0, 0, 0);
const object = new Group();
const physics = new OimoPhysicsBuffer({
gravity: center
});
for (let i = 0; i < 100; i++) {
object.position.x = MathUtils.randFloat(10, 100) * (headsTails() ? -1 : 1);
object.position.y = MathUtils.randFloat(10, 100) * (headsTails() ? -1 : 1);
object.position.z = MathUtils.randFloat(10, 100) * (headsTails() ? -1 : 1);
object.rotation.x = MathUtils.degToRad(MathUtils.randInt(0, 360));
object.rotation.y = MathUtils.degToRad(MathUtils.randInt(0, 360));
object.rotation.z = MathUtils.degToRad(MathUtils.randInt(0, 360));
object.updateMatrix();
physics.add({
name: `balls_${i}`,
type: 'sphere',
position: object.position.toArray(),
quaternion: object.quaternion.toArray(),
size: [1],
density: 1,
autoSleep: false
});
}
for (let i = 0; i < numPointers; i++) {
physics.add({
name: `sphere_${i}`,
type: 'sphere',
size: [1],
density: 1
});
}
for (let i = 0; i < numPointers; i++) {
physics.add({
name: `mouse_${i}`,
kinematic: true
});
}
for (let i = 0; i < numPointers; i++) {
physics.add({
name: `mouseJoint_${i}`,
type: 'joint',
mode: 'spherical',
body1: `sphere_${i}`,
body2: `mouse_${i}`,
springDamper: [15, 1]
});
}
for (let i = 0; i < numPointers; i++) {
resetMouse(i);
}
function resetMouse(index) {
const mouse = `mouse_${index}`;
const sphere = `sphere_${index}`;
const position = [0, 0, 101 + index]; // Initial position behind the camera
physics.setPosition(mouse, position);
physics.sleep(mouse);
physics.setPosition(sphere, position);
physics.sleep(sphere);
}
//
class Ball {
constructor(name) {
this.force = new Vector3();
this.forceDamping = 0.007;
this.forceThreshold = 0.3;
this.contact = false;
physics.setContactCallback(name, this.onContact);
}
// Event handlers
onContact = (body, name) => {
if (this.contact) {
return;
}
const linearVelocity = body.getLinearVelocity();
const mass = body.getMass();
this.force.addScaledVector(linearVelocity, mass);
this.force.multiplyScalar(this.forceDamping);
const force = this.force.length();
if (force > this.forceThreshold) {
this.contact = true;
const data = Buffer.allocUnsafe(6);
data.writeUInt8(3, 0);
data.writeUInt8(parseInt(name.match(/\d+$/)[0], 10), 1);
data.writeFloatBE(force, 2);
// console.log('CONTACT:', name, data);
broadcast(null, data);
setTimeout(() => {
this.contact = false;
}, 250);
} else {
this.force.multiplyScalar(0);
}
};
}
for (let i = 0; i < 100; i++) {
new Ball(`balls_${i}`);
}
//
import { performance } from 'node:perf_hooks';
const force = new Vector3();
const resetOrientation = [0, 0, 0, 1];
const resetVelocity = [0, 0, 0];
const timestep = 1000 / 61;
const byteLength = 8 * 4; // 8 * float32 for buffer size
const startIndex = 1 + byteLength * 100; // event + size * balls
let startTime = 0;
let timeout = null;
function onUpdate() {
startTime = performance.now();
// Zero-G impulse applied to the balls towards the centre of the scene
for (let i = 0; i < 100; i++) {
const body = physics.bodies[i];
object.position.copy(body.getPosition());
object.quaternion.copy(body.getOrientation());
object.updateMatrix();
force.copy(object.position).negate().normalize().multiplyScalar(0.1);
body.applyImpulse(force, center);
}
physics.step();
const data = Buffer.allocUnsafe(1 + physics.array.buffer.byteLength); // event + size
data.writeUInt8(2, 0);
Buffer.from(physics.array.buffer).copy(data, 1);
// Overwrite sleeping index with isMove and isDown state
let index;
for (let i = 0, l = clients.length; i < l; i++) {
const client = clients[i];
if (client._mouse !== null) {
index = startIndex + byteLength * client._mouse;
data.writeFloatLE(
client._isMove ? client._isDown ? 2 : 1 : 0,
index + 28 // 7 * float32 for sleeping index
);
// Reset sphere orientation and velocities
const sphere = `sphere_${client._mouse}`;
physics.setOrientation(sphere, resetOrientation);
physics.setLinearVelocity(sphere, resetVelocity);
physics.setAngularVelocity(sphere, resetVelocity);
}
}
// console.log('BUFFER:', data);
broadcast(null, data);
if (timeout !== null) {
timeout = setTimeout(onUpdate, Math.max(0, timestep - (performance.now() - startTime)));
}
}