From 2b3b10e0fb651c60144b80cd1da939f7b79a0dbb Mon Sep 17 00:00:00 2001 From: Sylvain Gay Date: Thu, 8 Sep 2022 17:48:53 +0200 Subject: [PATCH] =?UTF-8?q?Pas=20d'erreur=20de=20code=20mais=20timeout=20s?= =?UTF-8?q?ur=20la=20g=C3=A9olocalisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + map.html | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 8 ++++ traque.js | 85 +++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 .gitignore create mode 100644 map.html create mode 100644 package.json create mode 100644 traque.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25c8fdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/map.html b/map.html new file mode 100644 index 0000000..d65285d --- /dev/null +++ b/map.html @@ -0,0 +1,132 @@ + + + + + + Leaflet - test + + + + + + + + + + + +

+ +
+
+ + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d668e7 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "traque", + "version": "0.0.1", + "description": "appli pour la traque", + "dependencies": { + "socket.io": "^4.5.2" + } +} diff --git a/traque.js b/traque.js new file mode 100644 index 0000000..919c906 --- /dev/null +++ b/traque.js @@ -0,0 +1,85 @@ +/* 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('http'); +var url = require('url'); +var fs = require('fs'); + +console.log("Setup http server"); + +// The server +var server = http.createServer(function(req, res){ + 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){ + socket.id = equipes.length; + equipes.push(socket); + socket.shown = false; + socket.color = 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}); + }); + socket.on('changeColor', function(d){ + socket.color = d.color; + 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.emit('yourId', {"id": socket.id}); +}); + +console.log("Launch server"); +server.listen(9000); +console.log("Running!");