-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
111 lines (100 loc) · 2.51 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
'use strict';
const path = require('path');
const express = require('express');
const app = express();
const http = require('http').Server(app);
const glob = require('glob');
const fs = require('fs');
const io = require('socket.io')(http);
const PORT = process.env.PORT || 3000;
app.use(express.static(__dirname));
app.use(express.static(path.resolve(__dirname, 'games')));
const controllers = glob.sync(__dirname + '/app/controllers/**/*.js');
controllers.forEach(function(controller) {
require(controller)(app);
});
const packingStructure = [
'id',
'timestamp',
// this should be replace/upgraded with a whitelist instead of a blacklist.
// leaving out r,s,y, and gestures
{hands: [[
'id',
'type',
'direction',
'palmNormal',
'palmPosition',
'palmVelocity',
'stabilizedPalmPosition',
'pinchStrength',
'grabStrength',
'confidence',
'armBasis',
'armWidth',
'elbow',
'wrist'
// leaving out r, s, t, sphereCenter, sphereRadius
]]},
{pointables: [[
'id',
'direction',
'handId',
'length',
'stabilizedTipPosition',
'tipPosition',
'tipVelocity',
'tool',
'carpPosition',
'mcpPosition',
'pipPosition',
'dipPosition',
'btipPosition',
'bases',
'type'
// leaving out touchDistance, touchZone
]]},
{interactionBox: [
'center', 'size'
]}
];
function leapRecorder(socket){
let fileName = socket.id.substring(socket.nsp.name.length + 1);
fileName = socket.nsp.name.substring(1) + '-' + fileName + '.json';
const ws = fs.createWriteStream(fileName);
const fileData = {
metadata: {
formatVersion: 2,
generatedBy: 'Socket.io saver',
frames: 0,
protocolVersion: 6,
frameRate: '1.1e+2',
modified: new Date()
},
frames: [packingStructure]
}
const str = JSON.stringify(fileData);
ws.write(str.substring(0, str.length - 2));
socket.on('disconnect', () => {
ws.write(']}');
ws.close();
});
socket.on('frameData', (data) => {
ws.write(',' + JSON.stringify(data.frameData));
});
socket.on('frameBuffer', data => {
let buffer = JSON.stringify(data[0]);
for (let i = 1; i < data.length; i++) {
buffer = buffer + ',' + JSON.stringify(data[i]);
}
ws.write(',' + buffer);
});
}
io.of('/catch-stars')
.on('connection', leapRecorder);
io.of('/runner-boy')
.on('connection', leapRecorder);
io.of('cubes-road')
.on('connection', leapRecorder);
http.listen(PORT, function () {
console.log(`'App listening on port ${PORT}!'`);
});