
var express = require('express'); // express node framework
var app = express();
var server = app.listen(3000);//localhost na porte 3000
var clients = [];

app.use(express.static('public')); //zdiela public dir
console.log("Server nastartovany");
var socket = require('socket.io');
var io = socket(server);
io.sockets.on('connection',newConnection);

// --------------- OSC ----------------------------
var osc = require('node-osc');
var client = new osc.Client('127.0.0.1', 12000);
function listClients(){
  console.log("Client list " + clients);
}
function newConnection(socket){
  var id = socket.id;
  console.log("New Connection " + id);
  clients.push(id);
  client.send('/users',clients.length);
  listClients();
  socket.on('ballPos',ballMsg);
  socket.on('kamera',kameraMsg);
  socket.on('matter',matterMsg);
  socket.on('vodovaha',vodovahaMsg);
  socket.on('multitouch',multitouchMsg);
  socket.on('multitouchLast',multitouchLastMsg);
  socket.on('disconnect', function () {
    clients.splice(clients.indexOf(id),1);
    console.log("Disconnected " + id);
    listClients();
    client.send('/users',clients.length);
    });
  function matterMsg(data){
    client.send('/matterOSC', data);
  }
  function vodovahaMsg(data){
    client.send('/vodovahaOSC', data);
  }
  function multitouchMsg(data){
    client.send('/multitouchOSC', data);
  }
  function multitouchLastMsg(data){
    client.send('/multitouchLastOSC', data);
  }
  function ballMsg(data){
    client.send('/loptyOSC', data);
  }
  function kameraMsg(data){
    client.send('/kameraOSC', data);
  }
}
