-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
84 lines (67 loc) · 2.48 KB
/
index.html
File metadata and controls
84 lines (67 loc) · 2.48 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
test SocketIO
<table class="table">
<thead>
<tr>
<th>User</th>
<th>Comment</th>
</tr>
</thead>
<tbody id="rows">
</tbody>
</table>
<div id="messages"></div>
<button id="broadcast"> test</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var port = window.location.port,
socket = io.connect('http://localhost:' + port);
// var socket = io('http://localhost:4000', {
// 'reconnection': true,
// 'reconnectionDelay': 500,
// 'reconnectionAttempts': 5
// });
socket.on('comment.add', function (message) {
console.log(message.length)
message.forEach(element => {
$('#rows').append(`<tr>
<td>${element.user}</td>
<td>${element.comment}</td>
</tr>`)
});
socket.on('reconnect', function (number) {
console.info('After attempting ' + number + ' times, we finally reconnected!');
});
socket.on('reconnect_attempt', function (number) {
console.info('Reconnect attempt number ' + number);
});
socket.on('connect_error', function () {
console.warn('Error connecting to Socket.IO');
});
socket.on('reconnect_failed', function () {
console.error('We failed to reconnect to Socket.IO. We give up.');
});
})
// Add new messages to the list
socket.on('message.received', function (port) {
var message = document.createElement('div');
message.innerHTML = `Received message from port ${port}`;
document.getElementById('messages').appendChild(message);
});
// When the "broadcast" button is clicked,
// We will send a message to the server to render the message
document.getElementById('broadcast').addEventListener('click', function () {
socket.emit('message.sent', port);
});
</script>
</body>
</html>