-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnxserver.js
More file actions
160 lines (124 loc) · 4.76 KB
/
nxserver.js
File metadata and controls
160 lines (124 loc) · 4.76 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
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
/* This server enables nexusUI communication
over a node websocket using socket.io
With node.js installed,
start the server with the command:
node nxserver.js
*/
////////////////////////////////////////////////////////////////////////////////
// set up basic connectivity
////////////////////////////////////////////////////////////////////////////////
// web based stuff
var connect = require('connect'),
http = require('http'),
// set up serving of static pages from this folder
app = connect().use(connect.static(__dirname)).listen(8080),
// set up web service port
io = require('socket.io').listen(app);
console.log('http server on 8080');
// osc client
var osc = require('node-osc'),
oscClient = new osc.Client('localhost', 57120);
console.log('osc client on 57120');
// osc server
var oscServer = new osc.Server(4444, '0.0.0.0');
console.log('osc server on 4444');
////////////////////////////////////////////////////////////////////////////////
// widget management and messaging to web clients is handled here
////////////////////////////////////////////////////////////////////////////////
// a collection to store info about our widgets - this cache is used to update
// new clients upon connection
var widgets = {};
// creates a widget
function createWidget(name, type, x, y, w, h) {
// update our cache of widget information
widgets[name] = {
'name': name,
'type': type,
'x': x, 'y': y,
'w': w, 'h': h,
'properties': {}
};
// update all web clients
io.sockets.emit('create', widgets[name]);
console.log('widget ' + name + ' created.')
}
// sets a widgets property with value
function updateWidget(name, property, value) {
widgets[name]['properties'][property] = value;
// update the web clients
io.sockets.emit('update', name, property, value);
console.log('widget ' + name + ' ' + property + ' set to ' + value);
}
// deletes a widget
function deleteWidget(name) {
// update our cache of widget information
delete widgets[name];
// update all web clients
io.sockets.emit('delete', name);
console.log('widget ' + name + ' deleted.')
}
// send all of the information about widgets to client on socket
function sendClientAllWidgetInfo(socket) {
for(var w in widgets) {
// send creation message
socket.emit('create', widgets[w]);
// and widgets properties
for(var p in widgets[w].properties) {
socket.emit(
'update', w, p, widgets[w]['properties'][p]
);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// handle incoming data from OSC
////////////////////////////////////////////////////////////////////////////////
oscServer.on('message', function(oscMsg, rinfo) {
console.log(oscMsg);
var oscPath = oscMsg[0],
args = oscMsg.slice(1, oscMsg.length);
switch(oscPath) {
// creates/updates a new widget
// e.g. /nexus/widget mySlider slider 0 0 100 100
case '/nexus/create':
createWidget.apply(this, args);
break;
// deletes a widget
// e.g. /nexus/delete mySlider
case '/nexus/delete':
deleteWidget(args[0]);
break;
// update a widgets property
// e.g. /nexus/update mySlider 0.5
case '/nexus/update':
updateWidget.apply(this, args);
break;
// this message allows the osc client being sent data about widget
// values to be changed
case '/nexus/osc_client':
oscClient.host = oscMsg[1];
oscClient.port = oscMsg[2];
console.log(
'Now sending OSC data to: ' + oscMsg[1] + ':' + oscMsg[2]
);
break;
}
})
////////////////////////////////////////////////////////////////////////////////
// handle incoming data from web page
////////////////////////////////////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
// when a new client connects tell them about the existing widgets
sendClientAllWidgetInfo(socket);
// when we see OSC data coming back from a widget send it to osc client
// by default nexus widgets send back info to nx
socket.on('nx', function(data) {
oscClient.send(data.oscName, data.value);
// work out which widget param is being set
var splitOscName = data.oscName.split('/');
// update our cache with that info
widgets[splitOscName[1]].properties[splitOscName[2]] = data.value;
// tell other clients about change
socket.broadcast.emit('update', splitOscName[1], splitOscName[2], data.value);
});
});