-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·178 lines (132 loc) · 4.19 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
#!/usr/bin/env node
var fs = require('fs');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var httpRequest = require('http-request');
var url = require("url");
var BufferedNetstringStream = require('./netstring').BufferedNetstringStream;
var colors = require('colors/safe');
var verbose = true;
var portNum = 5600;
var clients = [];
var urlRules = [
['.png','image/png'],
['.js','application/javascript'],
['.html','text/html'],
['.css','text/css'],
['.json','text/event-stream'],
['.xml','text/xml'],
['.svg','image/svg+xml'],
['.ttf','application/x-font-ttf'],
['.otf','application/x-font-opentype'],
['.woff','application/x-font-woff'],
['.eot','application/vnd.ms-fontobject'],
['.ico','image/x-icon']
];
// Init kinect stream functions
var id = 0;
function writeImage(image) {
var uri = 'data:image/png;base64,' + image.toString('base64');
clients.forEach(function(client) {
client.write('id: ' + id + '\n');
client.write('data: ' + uri + '\n\n');
});
id++;
}
function stream(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
clients.push(res);
req.on('close', function() {
var index = clients.indexOf(res);
if (index != -1) {
clients.splice(index, 1);
}
});
}
// Start Express webserver
app.get('*', function(req, res) {
var parsedURL = url.parse(req.url, true);
var pathname = parsedURL.pathname;
var reqURL = __dirname + req.url;
if(pathname == '/') reqURL = __dirname + '/webroot/index.html';
else if(pathname == '/ui') reqURL = __dirname + '/webroot/ui.html';
if (pathname == '/images') {
stream(req, res, 'text/event-stream');
if(verbose) console.log('kinect stream requested');
} else {
var mimeType = false;
for(var i=0; i< urlRules.length; i++) {
if (reqURL.indexOf(urlRules[i][0]) > -1) {
mimeType = urlRules[i][1];
i = urlRules.length;
}
}
if(!mimeType) mimeType = 'text/plain';
fs.readFile(reqURL, function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading ' +reqURL);
console.log('Error loading ' +reqURL);
}
res.writeHead(200,{
'Content-Type': mimeType
});
res.end(data);
});
}
});
// Set up Socket.IO connections
io.on('connection', function (socket) {
socket.on('deviceActive', function (data) {
console.log('New socket opened for device "' + data.deviceName + '"');
});
socket.on('effectList', function (data) {
io.emit('effectList', data);
if(data.request == 'SET') console.log('Effect list received!', data.data.effectList);
});
socket.on('effectParams', function (data) {
io.emit('effectParams', data);
if(data.request == 'SET') console.log('Effect params received!', data.data);
});
socket.on('filterParams', function (data) {
io.emit('filterParams', data);
if(data.request == 'SET') console.log('Filter params received!', data.data);
});
socket.on('calibrationParams', function (data) {
io.emit('calibrationParams', data);
if(data.request == 'SET') console.log('Calibration params received!', data.data);
});
socket.on('effectChanged', function (data) {
io.emit('effectChanged', data);
if(data.request == 'SET') console.log('Effect change received!', data.data);
});
socket.on('serviceStatus', function (data) {
io.emit('serviceStatus', data);
if(data.request == 'SET') console.log('Service status sent from ' + data.deviceName + ' for ' + data.data.service, data.data);
});
socket.on('currentEffectParams', function (data) {
io.emit('currentEffectParameters', data);
});
socket.on('paramUpdated', function(data) {
io.emit('paramUpdated', data);
if(data.request == 'SET') console.log('Param updated from ' + data.deviceName);
});
socket.on('disconnect', function () {
io.emit('user disconnected');
console.log('IO Socket disconnected');
});
});
// Launch server
process.stdin.resume();
process.stdin.pipe(new BufferedNetstringStream).on('data', writeImage);
http.listen(portNum);
console.log(colors.green.bold('Node server started!'));
setTimeout(function() {
console.log('socket io sending fake get effect list request')
io.emit('effectList', {request: 'GET'});
},3000);