/* struct equipe { "name" : string, "pos" : [lat : float, long : float], "color" : int, "socket" : socket "shown" : bool } Les messages à transmettre par le client : - geoLoc(position) - changeColor(color) - setName(id) Les messages à transmettre par le serveur : - moving(id, position) - changeColor(id, color) - setName(id, name) */ var equipes = []; // require = include var http = require('https');//require('http'); var url = require('url'); var fs = require('fs'); var config = require('./config.js'); console.log("Setup http server"); const option = { key: fs.readFileSync(config.key), cert: fs.readFileSync(config.cert) }; // The server var server = http.createServer(option, function(req, res){ var q = url.parse(req.url, true); var filename = "static" + q.pathname; if(q.pathname.includes("..")) filename = "static/dotdot.html"; if(q.pathname == "/") filename = "map.html"; fs.readFile(filename, function(err, data) { if (err) { res.writeHead(404, {'Content-Type': 'text/html'}); return res.end("404 Not Found"); } res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); return res.end(); }); }); console.log("Setup io server"); const { Server } = require("socket.io"); var io = new Server(server); console.log("Setup handlers"); io.sockets.on('connection', function(socket){ console.log("connection !"); socket.id = equipes.length; equipes.push(socket); socket.shown = true; socket.color = 0; socket.position = [0,0]; socket.on('geoLoc', function(d){ socket.position = d.position; if(socket.shown) for(i in equipes) equipes[i].emit('moving', {"id": socket.id, "position": d.position}); else socket.emit('moving', {"id": socket.id, "position": d.position}); }); socket.on('changeColor', function(d){ socket.color = d.color - 0; if(d.color == -1) socket.shown = false; else{ if(!socket.shown) for(i in equipes) equipes[i].emit('moving', {"id": socket.id, "position": socket.position}); socket.shown = true; } for(i in equipes) equipes[i].emit('changeColor', {"id": socket.id, "color": d.color}); }); socket.on('setName', function(d){ socket.name = d.name; for(i in equipes) equipes[i].emit('setName', {"id": socket.id, "name": d.name}); }); socket.on("disconnect", function(_){ socket.shown = false; socket.color = -1; for(i in equipes) equipes[i].emit('changeColor', {"id": socket.id, "color": -1}); }); socket.emit('yourId', {"id": socket.id}); for(i in equipes){ if(!equipes[i].shown) continue; socket.emit('setName', {"id": i, "name": equipes[i].name}); socket.emit('changeColor', {"id": i, "color": equipes[i].color}); socket.emit('moving', {"id": i, "position": equipes[i].position}); } }); console.log("Launch server"); server.listen(config.port, "::"); console.log("Running !");