connection des deux client

This commit is contained in:
2026-07-30 23:47:59 +02:00
parent c975fb87ea
commit 1b00a35e6c
2 changed files with 21 additions and 3 deletions

View File

@@ -7,5 +7,10 @@
<body> <body>
<h1>Bonjour !</h1> <h1>Bonjour !</h1>
<p>Ceci est une page statique servie par Express.</p> <p>Ceci est une page statique servie par Express.</p>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
console.log("Connecte au serveur, id :", socket.id);
</script>
</body> </body>
</html> </html>

View File

@@ -1,12 +1,25 @@
const express = require('express'); const express = require('express');
const path = require('path'); const path = require('path');
const http = require('http');
const { Server } = require('socket.io');
const app = express(); const app = express();
const server = http.createServer(app);
const io = new Server(server);
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
// Sert tous les fichiers du dossier "public" // Sert tous les fichiers du dossier "client"
app.use(express.static(path.join(__dirname, '../client'))); app.use(express.static(path.join(__dirname, "..", "client")));
app.listen(PORT, () => { io.on("connection", (socket) => {
console.log("Un client s'est connecte : ${socket.id}");
socket.on("disconnect", () => {
console.log("Client deconnecte : ${socket.id}");
});
});
server.listen(PORT, () => {
console.log(`Serveur démarré sur http://localhost:${PORT}`); console.log(`Serveur démarré sur http://localhost:${PORT}`);
}); });