-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
71 lines (67 loc) · 2.03 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<title>Chat App</title>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #eeeeee;
}
#messages {
height: 400px;
overflow: auto;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
}
#messages p {
padding: 5px;
border-radius: 4px;
margin-bottom: 5px;
background-color: #fff;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<div class='container'>
<h1 class='mt-5 mb-5 text-center'>Chat App</h1>
<div class='card mb-4 shadow'>
<div class='card-body' id='messages'></div>
</div>
<form id="form" action="" class="form-inline">
<input id='u' autocomplete='off' class='form-control mb-3 mr-2' placeholder='Username' />
<input id='m' autocomplete="off" id='m' autocomplete='off' class='form-control mb-3 mr-2' placeholder='Type a message...' />
<button type='submit' class='btn btn-primary'>Send</button>
</form>
</div>
<script src='/socket.io/socket.io.js'></script>
<script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
<script>
const socket = io();
$('form').submit(function(e) {
e.preventDefault();
var nickname = $('#u').val();
if(nickname == null || nickname == '') {
alert('Enter your nickname!');
}
else {
socket.emit('message', { sender: nickname, receiver: 'User2', text: $('#m').val() });
$('#m').val('');
$('#n').attr('readonly', true);
}
});
socket.on('message', function(msg) {
$('#messages').append($('<p>').text(msg.sender + ': ' + msg.text));
});
socket.emit('history', { user1: 'User1', user2: 'User2' });
socket.on('history', function(messages) {
$('#messages').empty();
for(let message of messages) {
$('#messages').append($('<p>').text(message.sender + ': ' + message.text));
}
});
</script>
</body>
</html>