1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- const express = require('express');
- const dgram = require('dgram');
- const oscParser = require('./osc-parser');
- const bodyParser = require('body-parser');
- const path = require('path');
- const app = express();
- const server = require('http').Server(app);
- const io = require('socket.io')(server, {
- transports: ['websocket']
- });
- const onSocketListening = function() {
- const address = udpSocket.address();
- console.log("Serveur TUIO en écoute sur : " + address.address + ":" + address.port);
- };
- const onSocketConnection = function(socket) {
- udpSocket.on("message", function(msg) {
- socket.emit("osc", oscParser.decode(msg));
- });
- };
- const udpSocket = dgram.createSocket('udp4');
- udpSocket.on('listening', onSocketListening);
- udpSocket.bind(3333, '127.0.0.1');
- //io.listen(server);
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({extended:true}));
- app.use(express.static('./frontend/assets'));
- app.get('/', function (req, res) {
- res.sendFile('./frontend/index.html', { root: path.resolve(__dirname + '/..') })
- });
- app.get('/json', function (req, res) {
- res.status(200).json({"message":"ok"})
- });
- io.sockets.on('connection', (socket) =>{
- console.log(`Connecté au client ${socket.id}`);
- const dgramCallback = function (buf) {
- console.log(oscParser.decode(buf));
- socket.emit("osc", oscParser.decode(buf));
- };
- // forward UDP packets via socket.io
- udpSocket.on("message", dgramCallback);
- // prevent memory leak on disconnect
- socket.on('disconnect', function (socket) {
- udpSocket.removeListener('message', dgramCallback);
- });
- });
- //io.sockets.on("connection", onSocketConnection);
- // on change app par server
- server.listen(5000, function () {
- console.log('App frontend disponible sur localhost:5000 !')
- });
|