This repository was archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
78 lines (76 loc) · 2.67 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
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple chat</title>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.messages-chat {
height: 200px;
overflow-y: scroll;
border: 1px rgba(78, 71, 77, 0.9) solid;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-6 mx-auto p-4">
<form>
<div class="messages-chat w-100 mb-1">
<ul class="list-inline pl-1 p-1 w-100">
</ul>
</div>
<div class="row">
<div class="col-10 mr-0">
<input name="message" class="form-control" value="" type="text"
placeholder="Type the message...">
</div>
<div class="col-2 ml-0 pl-0">
<input type="button" class="form-control btn-primary" value="Send">
</div>
</div>
</form>
</div>
</div>
</div>
<script>
var historyFlag = true;
var conn = new WebSocket('ws://localhost:8080');
//Create connection
conn.onopen = function (e) {
console.log("Connection established!");
};
//Event after send message from everyone!
conn.onmessage = function (e) {
console.log(e.data);
datas = JSON.parse(e.data);
if (historyFlag == true) {
historyFlag = false;
if (datas.last_messages.length > 0) {
for (var i in datas.last_messages) {
var item = JSON.parse(datas.last_messages[i]);
$('div.messages-chat ul').append('<li class="border-bottom">' + item.message + '</li>');
}
}
}
if (datas.message) {
$('div.messages-chat ul').append('<li class="border-bottom">' + datas.message + '</li>');
}
};
$(document).ready(function () {
$('input[type=button]').click(function (e) {
e.preventDefault();
var data = {};
data.message = $("input[name=message]").val();
$('div.messages-chat ul').append('<li class="border-bottom">' + data.message + '</li>');
conn.send(JSON.stringify(data));
});
})
</script>
</body>
</html>