1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //--------------------------------------------------
- // Bi-Directional OSC messaging Websocket <-> UDP
- //--------------------------------------------------
- var osc = require("osc"),
- fs = require('fs'),
- Https = require('https'),
- WebSocket = require("ws");
- const httpsServer = Https.createServer({
- key: fs.readFileSync('./certs/192.168.0.10.key'),
- cert: fs.readFileSync('./certs/192.168.0.10.pem')
- });
- 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;
- };
- var udp = new osc.UDPPort({
- localAddress: "0.0.0.0",
- localPort: 7400,
- remoteAddress: "127.0.0.1",
- remotePort: 4559 //7500 4559 - for Sonic Pi
- });
- udp.on("ready", function () {
- var ipAddresses = getIPAddresses();
- console.log("Listening for OSC over UDP.");
- ipAddresses.forEach(function (address) {
- console.log(" Host:", address + ", Port:", udp.options.localPort);
- });
- console.log("Broadcasting OSC over UDP to", udp.options.remoteAddress + ", Port:", udp.options.remotePort);
- });
- udp.open();
- var wss = new WebSocket.Server({
- server: httpsServer
- //port: 8081
- });
- httpsServer.on('request', (req, res) => {
- res.writeHead(200);
- res.end('WebSockets through HTTPS\n');
- });
- wss.on("connection", function (socket) {
- console.log("A Web Socket connection has been established!");
- var socketPort = new osc.WebSocketPort({
- socket: socket
- });
- var relay = new osc.Relay(udp, socketPort, {
- raw: true
- });
- });
- httpsServer.listen(8081);
|