/
index.js
143 lines (116 loc) · 3.37 KB
/
index.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
const express = require('express');
const app = express();
const osc = require('osc')
const config = require('./config.js');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const fs = require('fs')
const net = require('net')
const OSCServer = new osc.UDPPort({
localAddress: config.OSCLocalAddr,
localPort: config.OSCLocalPort,
remoteAddress: config.SCAddr,
remotePort: config.SCPort,
metadata : false
});
OSCServer.open();
console.log("reaading plugins dir.")
let plugins = {}
fs.readdirSync('./'+config.PluginsDir).forEach((file)=>{
if (file.endsWith('.js')){
try{
let plugin = require(config.PluginsDir+'/'+file)
if (Object.keys(plugin).includes("address") && Object.keys(plugin).includes('callback')){
plugins[plugin.address] = plugin.callback
}
}catch(error){
console.log("error loading "+file+" \n "+error)
}
}
})
console.log(`plugins loaded: "${Object.keys(plugins).join(", ")}"`)
OSCServer.on('ready', function(){
console.log('OSC started.');
io.on('connection', function(socket){
console.log('Connected.');
const autosaved = fs.readFile("autosaved.json",'utf8', (error, data)=>{
if(error){
socket.emit('load','','')
}else{
const dataparsed = JSON.parse(data)
//socket.emit('load',dataparsed.texteditor, dataparsed.parsereditor)
}
})
socket.on('plugin', function(plugin, ...args){
if(Object.keys(plugins).includes(plugin) ){
let syncFunc= new Promise((resolve, reject)=>{
try{
let result = plugins[plugin].bind(
{
socket: socket,
io: io,
emit: function(...params){
socket.emit(`plugin-${plugin}`, ...params) }
}
)(...args)
resolve(`plugin ${plugin}: ${result}`);
}catch(error){
error.message = `error on plugin ${plugin}: ${error.message}`
reject(error)
}
}).then((log)=>{
console.log(log)
}).catch((error)=>{
console.log("Error in plugin", error)
})
}
})
socket.on('save',function(text, parser){
fs.writeFile("autosaved.json",
JSON.stringify(
{
texteditor: text,
parsereditor: parser
}),(err)=>{
if(err) console.log(err,"error saving 'autosaved.json'")
})
})
socket.on('osc',function(...args){
let opts = {
ip: config.SCAddr,
port: config.SCPort
};
const address = args[0].startsWith('/')? args[0] :'/'+args[0] ;
if(args.length == 2 && typeof args[1] == 'object' && !Array.isArray(args[1])){
for(i in args[1]){
opts[i] = args[1][i];
}
args = Array.isArray(args[1].args) ? args[1].args : [args[1].args];
}else{
args = args.splice(1)
}
let oscargs = args.map((arg)=>{
if(Number.isInteger(arg)){
return {type: 'i', value: arg}
}else if(typeof arg == "number"){
return {type: 'f', value: arg}
}else if(typeof arg == "string"){
return {type: 's', value: arg}
}else{
return {type: 's', value: JSON.stringify(arg)}
}
});
console.log(`sending: ${opts.ip}:${opts.port} -> ${address}, ${args}`)
OSCServer.send({
address: address,
args: oscargs
}, opts.ip, opts.port);
})
});
})
server.listen(8000);
app.use(express.static(__dirname+'/static'));
app.use(express.static(__dirname+'/views'));
let webserver = app.listen(3000, function () {
console.log("webserver started, listening on port 3000");
});