-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
46 lines (46 loc) · 1.33 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
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<link rel="stylesheet" type="text/css" href="/css/index.css">
</head>
<body>
<div><ul id="nrusers"></ul></div>
<ul id="messages"></ul>
<form action="" id="form">
<input type="text" id="m" autocomplete="off" required placeholder="Who are you?"/>
<button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
$(function () {
var socket = io();
var nick = "Anon";
var name = true;
$('#form').submit(function(e){
e.preventDefault(); // prevents page reloading
if(name) {
socket.emit('name', $('#m').val());
nick = $('#m').val();
$('#m').attr('placeholder', 'What you want to say?');
name = false;
} else {
socket.emit('chat message', $('#m').val());
}
$('#m').val('');
return false;
});
socket.on('chat message', function(name, msg){
$('#messages').append($('<li><b>' + name + '</b>' + msg + '</li>' ));
});
socket.on('server message', function(msg){
$('#messages').append($('<li id=\'serverm\'>').text(msg));
});
socket.on('nrusers', (msg) => {
$('#nrusers').append('<p>').text('Online: ' + msg);
});
});
</script>
</body>
</html>