-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (62 loc) · 2.18 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
var express = require('express'),
// , express.static(__dirname, + "/public")
// app = express.createServer(),
app=express(),
http=require('http'),
server = http.createServer(app),
io=require('socket.io').listen(server, {log:false}),
osc=require('node-osc/lib/osc.js'),
st = require('node-static'),
fs=require('fs');
io.set('log level', 1);
var oscServer, oscClient;
oscServer = new osc.Server(3333, '0.0.0.0'); //listening on 3333
// oscClient = new osc.Cliend('127.0.0.1', 3333);
// oscClient.send('/connection', 1);
var connectedSockets=[];
oscServer.on('message', function(msg, rinfo){
// console.log(msg[2]);
connectedSockets.forEach(function(s){
s.emit("oscdata", msg[2]);
})
})
server.listen(8080);
console.log("Server started on port 8080.");
app.use(express.static(__dirname + "/public"));
app.get('/getDirectoryInfo', function (req, res) {
var path = require("path"), self = this;
self.myfiles=[];
var rootDir = "./public/" + req.query.parentDir;
fs.readdir(rootDir,function(err,files){
if(err) throw err;
files.sort(function(a, b) {
if(!fs.statSync( path.join(rootDir + a) ).isDirectory()){
return;
}
if(!fs.statSync( path.join(rootDir + b) ).isDirectory()){
return;
}
return fs.statSync(rootDir + b).mtime.getTime() - fs.statSync(rootDir + a).mtime.getTime();
});
files.forEach(function(file){
if (!isUnixHiddenPath(file)) {
if (!fs.statSync( path.join(rootDir + file) ).isDirectory()){
self.myfiles.push({"name": file, "fullPath": req.query.parentDir + file, "dateAdded": fs.statSync(rootDir + file).mtime.getTime()});
}
}
});
res.json({ directories: self.myfiles })
});
});
var isUnixHiddenPath = function (path) {
return (/(^|\/)\.[^\/\.]/g).test(path);
};
io.sockets.on('connection', function(socket){
connectedSockets.push(socket);
console.log('a new connection, num of connections is '+(connectedSockets.length));
socket.on('disconnect', function(){
var idx = connectedSockets.indexOf(socket);
connectedSockets.splice(idx,1);
console.log('disconnected, remaining connections: '+ (connectedSockets.length));
});
})