Spaces:
Sleeping
Sleeping
Create static/index.html
Browse files- static/index.html +119 -0
static/index.html
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="fr">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Client WebSocket Simple</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 10 |
+
margin: 40px;
|
| 11 |
+
background-color: #f0f2f5;
|
| 12 |
+
}
|
| 13 |
+
.container {
|
| 14 |
+
max-width: 600px;
|
| 15 |
+
margin: auto;
|
| 16 |
+
padding: 20px;
|
| 17 |
+
background-color: #fff;
|
| 18 |
+
border-radius: 8px;
|
| 19 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 20 |
+
}
|
| 21 |
+
#status {
|
| 22 |
+
padding: 10px;
|
| 23 |
+
border-radius: 5px;
|
| 24 |
+
font-weight: bold;
|
| 25 |
+
margin-bottom: 15px;
|
| 26 |
+
}
|
| 27 |
+
.connected {
|
| 28 |
+
background-color: #e6ffed;
|
| 29 |
+
color: #2f6f43;
|
| 30 |
+
}
|
| 31 |
+
.disconnected {
|
| 32 |
+
background-color: #ffeef0;
|
| 33 |
+
color: #c53030;
|
| 34 |
+
}
|
| 35 |
+
#logs {
|
| 36 |
+
list-style-type: none;
|
| 37 |
+
padding: 0;
|
| 38 |
+
margin-top: 20px;
|
| 39 |
+
background-color: #f7f7f7;
|
| 40 |
+
border: 1px solid #ddd;
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
height: 200px;
|
| 43 |
+
overflow-y: scroll;
|
| 44 |
+
padding: 10px;
|
| 45 |
+
}
|
| 46 |
+
#logs li {
|
| 47 |
+
padding: 5px;
|
| 48 |
+
border-bottom: 1px solid #eee;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
</head>
|
| 52 |
+
<body>
|
| 53 |
+
|
| 54 |
+
<div class="container">
|
| 55 |
+
<h2>Client WebSocket pour API Mock</h2>
|
| 56 |
+
<div id="status" class="disconnected">Déconnecté</div>
|
| 57 |
+
|
| 58 |
+
<h3>Logs de Communication :</h3>
|
| 59 |
+
<ul id="logs">
|
| 60 |
+
<li>En attente de connexion...</li>
|
| 61 |
+
</ul>
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
<script>
|
| 65 |
+
const statusDiv = document.getElementById('status');
|
| 66 |
+
const logsList = document.getElementById('logs');
|
| 67 |
+
let ws;
|
| 68 |
+
|
| 69 |
+
function addLog(message) {
|
| 70 |
+
const li = document.createElement('li');
|
| 71 |
+
li.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
|
| 72 |
+
logsList.appendChild(li);
|
| 73 |
+
logsList.scrollTop = logsList.scrollHeight; // Auto-scroll
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function connect() {
|
| 77 |
+
// Adapte le protocole (ws ou wss pour le sécurisé)
|
| 78 |
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 79 |
+
const wsUrl = `${protocol}//${window.location.host}/ws`;
|
| 80 |
+
|
| 81 |
+
ws = new WebSocket(wsUrl);
|
| 82 |
+
|
| 83 |
+
ws.onopen = function() {
|
| 84 |
+
statusDiv.textContent = 'Connecté';
|
| 85 |
+
statusDiv.className = 'connected';
|
| 86 |
+
addLog('Connexion WebSocket établie.');
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
ws.onmessage = function(event) {
|
| 90 |
+
const messageFromServer = event.data;
|
| 91 |
+
addLog(`Message reçu du serveur : "${messageFromServer}"`);
|
| 92 |
+
|
| 93 |
+
// C'est ici que le client répond automatiquement
|
| 94 |
+
const responsePhrase = "Ceci est la phrase de réponse du client JavaScript.";
|
| 95 |
+
addLog(`Envoi de la réponse automatique : "${responsePhrase}"`);
|
| 96 |
+
ws.send(responsePhrase);
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
ws.onclose = function() {
|
| 100 |
+
statusDiv.textContent = 'Déconnecté';
|
| 101 |
+
statusDiv.className = 'disconnected';
|
| 102 |
+
addLog('Connexion WebSocket fermée. Tentative de reconnexion dans 3 secondes...');
|
| 103 |
+
// Tente de se reconnecter après 3 secondes
|
| 104 |
+
setTimeout(connect, 3000);
|
| 105 |
+
};
|
| 106 |
+
|
| 107 |
+
ws.onerror = function(error) {
|
| 108 |
+
addLog('Erreur WebSocket.');
|
| 109 |
+
console.error('WebSocket Error:', error);
|
| 110 |
+
ws.close();
|
| 111 |
+
};
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
// Lance la connexion au chargement de la page
|
| 115 |
+
connect();
|
| 116 |
+
</script>
|
| 117 |
+
|
| 118 |
+
</body>
|
| 119 |
+
</html>
|