index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //--------------------------------------------------
  2. // Bi-Directional OSC messaging Websocket <-> UDP
  3. //--------------------------------------------------
  4. var osc = require("osc"),
  5. fs = require('fs'),
  6. Https = require('https'),
  7. WebSocket = require("ws");
  8. const httpsServer = Https.createServer({
  9. key: fs.readFileSync('./certs/192.168.0.10.key'),
  10. cert: fs.readFileSync('./certs/192.168.0.10.pem')
  11. });
  12. var getIPAddresses = function () {
  13. var os = require("os"),
  14. interfaces = os.networkInterfaces(),
  15. ipAddresses = [];
  16. for (var deviceName in interfaces){
  17. var addresses = interfaces[deviceName];
  18. for (var i = 0; i < addresses.length; i++) {
  19. var addressInfo = addresses[i];
  20. if (addressInfo.family === "IPv4" && !addressInfo.internal) {
  21. ipAddresses.push(addressInfo.address);
  22. }
  23. }
  24. }
  25. return ipAddresses;
  26. };
  27. var udp = new osc.UDPPort({
  28. localAddress: "0.0.0.0",
  29. localPort: 7400,
  30. remoteAddress: "127.0.0.1",
  31. remotePort: 4559 //7500 4559 - for Sonic Pi
  32. });
  33. udp.on("ready", function () {
  34. var ipAddresses = getIPAddresses();
  35. console.log("Listening for OSC over UDP.");
  36. ipAddresses.forEach(function (address) {
  37. console.log(" Host:", address + ", Port:", udp.options.localPort);
  38. });
  39. console.log("Broadcasting OSC over UDP to", udp.options.remoteAddress + ", Port:", udp.options.remotePort);
  40. });
  41. udp.open();
  42. var wss = new WebSocket.Server({
  43. server: httpsServer
  44. //port: 8081
  45. });
  46. httpsServer.on('request', (req, res) => {
  47. res.writeHead(200);
  48. res.end('WebSockets through HTTPS\n');
  49. });
  50. wss.on("connection", function (socket) {
  51. console.log("A Web Socket connection has been established!");
  52. var socketPort = new osc.WebSocketPort({
  53. socket: socket
  54. });
  55. var relay = new osc.Relay(udp, socketPort, {
  56. raw: true
  57. });
  58. });
  59. httpsServer.listen(8081);