123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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');
- 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).send();
- });
- 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);
- });
- });
- // on change app par server
- server.listen(5000, function () {
- console.log('App frontend disponible sur localhost:5000 !')
- });
|