-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
53 lines (46 loc) · 1.37 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Micro Client-Server App</title>
</head>
<body>
<button id="getDataBtn">Get Data</button>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
document.getElementById('getDataBtn').addEventListener('click', () => {
socket.emit('getData');
});
socket.on('data', (data) => {
if (data.error) {
showErrorAlert(data.error);
} else {
showSuccessAlert(data);
}
});
socket.on('error', (error) => {
console.error('Socket error:', error);
showErrorAlert('Error communicating with the server');
});
function showErrorAlert(message) {
Swal.fire({
title: 'Error',
text: message,
icon: 'error',
confirmButtonText: 'OK'
});
}
function showSuccessAlert(data) {
Swal.fire({
title: 'Data from Server',
text: JSON.stringify(data),
icon: 'success',
confirmButtonText: 'OK'
});
}
</script>
</body>
</html>