-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (61 loc) · 1.89 KB
/
Copy pathserver.js
File metadata and controls
72 lines (61 loc) · 1.89 KB
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
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
osc = require('node-osc'),
fs = require('fs'),
url = require('url'),
path = require('path'),
open = require('open');
app.listen(process.env.PORT || 8081);
console.log('Server running at http://127.0.0.1:8081/');
// open('http://127.0.0.1:8081/');
function handler(req, res) {
var fpath = req.url;
var contentType = 'text/html';
var ua = req.headers['user-agent'];
var url_parts = url.parse(req.url);
var ext = path.extname(url_parts.pathname)
if (url_parts.pathname == '/dev') {
fpath = '/dev.html';
} else {
fpath = '/app.html';
}
switch (ext) {
case '.js':
fpath = url_parts.href;
contentType = 'text/javascript';
break;
case '.css':
fpath = url_parts.href;
contentType = 'text/css';
break;
case '.png':
fpath = url_parts.href;
contentType = 'image/png';
break;
}
fs.readFile(__dirname + fpath, function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading ' + __dirname + fpath);
}
res.writeHead(200, {
'Content-Type': contentType
});
res.end(data);
});
}
var oscServer, oscClient;
io.sockets.on('connection', function(socket) {
socket.on("config", function(obj) {
oscServer = new osc.Server(obj.server.port, obj.server.host);
oscClient = new osc.Client(obj.client.host, obj.client.port);
oscClient.send('/status', socket.sessionId + ' connected');
oscServer.on('message', function(msg, rinfo) {
console.log(msg, rinfo);
socket.emit("message", msg);
});
});
socket.on("message", function(obj) {
oscClient.send(obj);
});
});