2022-09-08 17:48:53 +02:00
|
|
|
/* 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
|
2022-09-09 13:03:08 +02:00
|
|
|
var http = require('https');//require('http');
|
2022-09-08 17:48:53 +02:00
|
|
|
var url = require('url');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
console.log("Setup http server");
|
|
|
|
|
2022-09-09 13:03:08 +02:00
|
|
|
const option = {
|
|
|
|
key: fs.readFileSync('certif/server.key'),
|
|
|
|
cert: fs.readFileSync('certif/server.crt')
|
|
|
|
};
|
|
|
|
|
2022-09-08 17:48:53 +02:00
|
|
|
// The server
|
2022-09-09 13:03:08 +02:00
|
|
|
var server = http.createServer(option, function(req, res){
|
2022-09-08 17:48:53 +02:00
|
|
|
var q = url.parse(req.url, true);
|
|
|
|
var filename = "." + q.pathname;
|
|
|
|
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){
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("connection !");
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.id = equipes.length;
|
|
|
|
equipes.push(socket);
|
2022-09-09 13:03:08 +02:00
|
|
|
socket.shown = true;
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.color = 0;
|
|
|
|
socket.on('geoLoc', function(d){
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("new loc :", d);
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.position = d.position;
|
|
|
|
if(socket.shown)
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('moving', {"id": socket.id, "position": d.position});
|
|
|
|
});
|
|
|
|
socket.on('changeColor', function(d){
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log(d);
|
|
|
|
socket.color = d.color - 0;
|
|
|
|
console.log("new color2");
|
2022-09-08 17:48:53 +02:00
|
|
|
if(d.color == -1)
|
|
|
|
socket.shown = false;
|
|
|
|
else{
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("new color3");
|
2022-09-08 17:48:53 +02:00
|
|
|
if(!socket.shown)
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('moving', {"id": socket.id, "position": socket.position});
|
|
|
|
socket.shown = true;
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("new color4");
|
2022-09-08 17:48:53 +02:00
|
|
|
}
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('changeColor', {"id": socket.id, "color": d.color});
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("new color5");
|
2022-09-08 17:48:53 +02:00
|
|
|
});
|
|
|
|
socket.on('setName', function(d){
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("new name");
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.name = d.name;
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('setName', {"id": socket.id, "name": d.name});
|
|
|
|
});
|
|
|
|
socket.emit('yourId', {"id": socket.id});
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Launch server");
|
|
|
|
server.listen(9000);
|
|
|
|
console.log("Running!");
|