-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
156 lines (129 loc) · 3.46 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" crossorigin="anonymous"></script>
<title>Socket.IO chat</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
#messages {
list-style-type: none;
margin: 0;
padding-left: 30px;
padding-right: 30px;
}
#messages li {
padding: 10px 25px;
font-size: 1.4em;
background: rgb(214, 235, 173);
border-radius: 20px;
margin-top: 10px;
}
#messages {
margin-bottom: 40px;
}
.bottomBar {
position: fixed;
bottom: 0;
width: 100%;
}
.bottomBar button {
float: left;
width: 15%;
}
.bottomBar form {
float: left;
width: 85%;
}
#messages li.me {
background-color: #EEF1BD !important;
text-align: right;
}
#messages li.connect {
background-color: #CCC !important;
text-align: center;
padding: 5px;
font-size: 1em;
}
#showTyping {
text-align: center;
background-color: #222;
color: #FFF;
padding: 5px;
visibility: hidden;
}
p {
text-align: right;
}
.jumbotron {
width: 50%;
margin: auto;
}
</style>
</head>
<body>
<div id="showTyping"></div>
<!-- Message list -->
<ul id="messages"></ul>
<!-- Bottom bar -->
<div class="bottomBar">
<div class="input-group">
<input id="m" class="form-control" autocomplete="off" />
<button onclick="fun()" id="send" class="btn btn-success">Send</button>
</div>
</div>
<!-- Set nickname -->
<div class="jumbotron" id="nickModal">
<h4>Set your nickname</h4>
<div class="input-group">
<input id="nickname" class="form-control" autocomplete="off">
<button onclick="addNick()" id="send" class="btn btn-success">Go!</button>
</div>
</div>
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
socket.on("chat", addMessage);
let nick;
function addNick() {
nick = $("#nickname").val();
$('#nickModal').css("display", "none");
getMessages();
}
function fun() {
var message = {
text: $("#m").val(),
user: nick
};
postMessage(message);
$('#m').val('');
}
function postMessage(message) {
$.post("http://localhost:3000/messages", message)
}
function getMessages() {
$.get("/messages", (chats) => {
chats.forEach(addMessage)
});
}
function addMessage(message) {
if (message.timestamp) {
$("#messages").append(`<li>${message.user}: ${message.text}<p><small>${message.timestamp}</small></p> </li>`);
} else {
$("#messages").append(`<li>${message.user}: ${message.text} </li>`);
}
}
</script>
</html>