var express = require('express'); var app = express(); var http = require('http').Server(app); var io = require('socket.io')(http); var mysql = require('mysql'); const util = require('util') var bodyParser = require('body-parser'); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies const config = require('./config.json'); //console.debug(config.bdd.user) var con = mysql.createConnection({ host: "localhost", user: config.bdd.user, password: config.bdd.pw, database : config.bdd.dbname }); app.set('view engine', 'ejs'); app.use('/static', express.static('static')); app.get('/', function(req, res){ res.render('index'); }).get('/TV', function(req, res){ res.render('TV'); }).get('/mapsTest', function(req, res){ con.query('SELECT * FROM zone', function (err, result, fields) { if (err) throw err; var data = JSON.stringify(result); data = JSON.parse(data); res.render('mapsTest', {data:data}); }); }).post('/addZone' , function(req, res){ var nord = req.body.N; var sud = req.body.S; var est = req.body.E; var ouest = req.body.O; var name = req.body.Name; con.query('CALL add_zone(?,?,?,?,?,?)', [true, name,nord,sud,ouest,est], function(err, result){ }); }); io.on('connection', function(socket){ socket.on('JoinZone', function(zone){ console.log("Join "+zone) socket.join(zone); }); socket.on('Communication', function(CommunicationJSON){ var data = JSON.parse(CommunicationJSON); console.log("JSON : "+CommunicationJSON); console.log("ZONE "+data.zone); io.sockets.in(data.zone).emit('Communication', CommunicationJSON); }); }); http.listen(1234, function(){ console.log('listening on *:1234'); });