This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsserver.js
67 lines (58 loc) · 1.7 KB
/
wsserver.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
var express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(7008),
net = require('net') ;
// We need a shuffle method for arrays
Array.prototype.shuffle = function() {
var s = [];
while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
while (s.length) this.push(s.pop());
return this;
}
// Some dummy test data
var TestData = new Array(8) ;
for (var i=0; i<TestData.length; i++) {
TestData[i] = new Array(2) ;
}
var Frc = new Array(256) ;
for (var i=0; i<Frc.length; i++) {
Frc[i] = i ;
}
// Load random value to dummy data
function ReloadTestData(data) {
Frc.shuffle() ;
for (var i=0; i<data.length; i++) {
data[i][0] = Frc[i] ;
data[i][1] = Math.floor(Math.random()*10) ;
}
return data ;
}
// little webserver for client html5 + javascript
app.set('view engine', 'jade') ;
app.use(express.static(__dirname + '/public')) ;
app.get('/', function(req, res) {
res.render('index', {host: req.headers.host.split(':')[0]}) ;
//res.sendfile(__dirname + '/index.html') ;
}) ;
app.listen(7007) ;
/* websocket server sending fft data
setInterval(function() {
TestData = ReloadTestData(TestData) ;
io.sockets.json.send(TestData) ;
}, 5000) ;
*/
// tcp server listening for fft data
var ffm_listener = net.createServer(function(socket) {
console.log("new connection") ;
socket.setEncoding('utf8') ;
socket.on('data', function(data) {
data = data.split('\;')[0].split('\\').join('') ; // TODO: check input!!!!
try {
data = JSON.parse(data) ; //TODO: fetch exception in a proper way
// TODO: check input size
io.sockets.json.send(data) ;
} catch(e) {
}
}) ;
}) ;
ffm_listener.listen(7000) ;