forked from ojack/LiveLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
165 lines (137 loc) · 5.64 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
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
161
162
163
164
165
/*--------------------------------------------------
// Bi-Directional OSC messaging Websocket <-> UDP
// ToDO: switch to using simpler OSC library following example: https://github.com/toddtreece/osc-examples/blob/master/socketio.js
//--------------------------------------------------
*/
var osc = require("osc"),
WebSocket = require("ws")
oscMin = require("osc-min");
var https = require('https');
var fs = require('fs');
var portscanner = require('portscanner');
var dgram = require('dgram');
var static = require('node-static');
var ports = {};
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var file = new static.Server('./public');
var udp_client = dgram.createSocket('udp4', function(msg, rinfo) {});
var httpsServer = https.createServer(options, function (request, response) {
request.addListener('end', function () {
//
// Serve files!
//
file.serve(request, response);
}).resume();
}).listen(8000);
var wss = new WebSocket.Server({
//port: 8081
server: httpsServer
});
var getIPAddresses = function () {
var os = require("os"),
interfaces = os.networkInterfaces(),
ipAddresses = [];
for (var deviceName in interfaces){
var addresses = interfaces[deviceName];
for (var i = 0; i < addresses.length; i++) {
var addressInfo = addresses[i];
if (addressInfo.family === "IPv4" && !addressInfo.internal) {
ipAddresses.push(addressInfo.address);
}
}
}
return ipAddresses;
};
wss.on("connection", function (ws) {
console.log("A Web Socket connection has been established!");
ws.on('message', function(data, flags) {
console.log("received data");
console.log(JSON.parse(data));
var message = JSON.parse(data);
if(message.type=="broadcastStream"){
if(message.port > 0 && message.port < 65536) {
// console.log("checking port " + message.port);
/*PORT SCANNER IS NOT WORKING TO CORRECTLY IDENTIFY PORTS CREATED USING osc.UDPPort
temporary fix is that previously open ports are stored in ports{} object*/
portscanner.checkPortStatus(parseInt(message.port), '127.0.0.1', function(error, status) {
// Status is 'open' if currently in use or 'closed' if available
if(ports.hasOwnProperty(message.port)){
console.log('port in use');
} else {
if(error) {
//TO DO: send error back to user
console.log("ERROR: "+error);
//reak;
} else {
if(status=="closed"){
ports[message.port] = "open";
portscanner.findAPortNotInUse(8000, 9000, '127.0.0.1', function(error, port) {
console.log('AVAILABLE PORT AT: ' + port)
addSocketUDPConnection(port, parseInt(message.port), message.name);
ws.send(JSON.stringify({"type": "new channel", "port": port, "udpPort": message.port}));
});
//addUDPServer(message.port, ws, message.name);
} else {
console.log("other status");
console.log(status);
}
}
}
});
} else {
console.log("not valid port");
}
} else if(message.type=="subscribeStream"){
//create osc client at specified port
// console.log("received data ");
// console.log(message.payload);
// console.log(message.payload);
var msg = oscMin.toBuffer(message.payload);
// console.log(msg);
udp_client.send(msg, 0, msg.length, message.port, "127.0.0.1", function(error){
if(error) console.log("UDP SEND ERROR", error);
});
// udp_client.send(JSON.stringify(message.payl))
}
// flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked.
});
});
//create a new socket<-->UDP connection
function addSocketUDPConnection(socketPort, udpPort, name){
var socketServer = https.createServer(options).listen(socketPort);
var socketChannel = new WebSocket.Server({
server: socketServer
});
console.log("adding udp port at "+ udpPort);
var udpChannel = new osc.UDPPort({
localAddress: "127.0.0.1",
localPort: udpPort
});
udpChannel.on("ready", function () {
var ipAddresses = getIPAddresses();
console.log("Listening for OSC over UDP.");
ipAddresses.forEach(function (address) {
console.log(" Host:", address + ", Port:", udpChannel.options.localPort);
portscanner.checkPortStatus(udpChannel.options.localPort, address, function(error, status) {
console.log("on ip "+ address + " port is "+ status);
});
});
});
udpChannel.on("osc", function(packet, info){
console.log(packet);
});
udpChannel.open();
socketChannel.on("connection", function(ws){
console.log("new connection from "+ socketPort + " to " + udpPort);
var socketOscPort = new osc.WebSocketPort({
socket: ws
});
var relay = new osc.Relay(udpChannel, socketOscPort, {
raw: true
});
});
}