server.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const express = require('express');
  2. const dgram = require('dgram');
  3. const oscParser = require('./osc-parser');
  4. const bodyParser = require('body-parser');
  5. const path = require('path');
  6. const app = express();
  7. const server = require('http').Server(app);
  8. const io = require('socket.io')(server, {
  9. transports: ['websocket']
  10. });
  11. const onSocketListening = function() {
  12. const address = udpSocket.address();
  13. console.log("Serveur TUIO en écoute sur : " + address.address + ":" + address.port);
  14. };
  15. const onSocketConnection = function(socket) {
  16. udpSocket.on("message", function(msg) {
  17. socket.emit("osc", oscParser.decode(msg));
  18. });
  19. };
  20. const udpSocket = dgram.createSocket('udp4');
  21. udpSocket.on('listening', onSocketListening);
  22. udpSocket.bind(3333, '127.0.0.1');
  23. //io.listen(server);
  24. app.use(bodyParser.json());
  25. app.use(bodyParser.urlencoded({extended:true}));
  26. app.use(express.static('./frontend/assets'));
  27. app.get('/', function (req, res) {
  28. res.sendFile('./frontend/index.html', { root: path.resolve(__dirname + '/..') })
  29. });
  30. app.get('/json', function (req, res) {
  31. res.status(200).json({"message":"ok"})
  32. });
  33. io.sockets.on('connection', (socket) =>{
  34. console.log(`Connecté au client ${socket.id}`);
  35. const dgramCallback = function (buf) {
  36. console.log(oscParser.decode(buf));
  37. socket.emit("osc", oscParser.decode(buf));
  38. };
  39. // forward UDP packets via socket.io
  40. udpSocket.on("message", dgramCallback);
  41. // prevent memory leak on disconnect
  42. socket.on('disconnect', function (socket) {
  43. udpSocket.removeListener('message', dgramCallback);
  44. });
  45. });
  46. //io.sockets.on("connection", onSocketConnection);
  47. // on change app par server
  48. server.listen(5000, function () {
  49. console.log('App frontend disponible sur localhost:5000 !')
  50. });