-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
172 lines (150 loc) · 6.24 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!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>Chat</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style>
body{
margin-top: 30px;
}
#messageArea{
display:none;
}
#userFormArea{
display:none;
}
</style>
</head>
<body>
<div class="container">
<div id="roomFormArea" class="row">
<div class="col-md-12">
<form id="roomForm">
<div class="form-group">
<label>Enter room number (6 Digits)</label>
<input class="form-control" id="room" placeholder="Leave empty if you create a room.">
</br>
<input type="submit" id="joinSub" class="btn btn-primary" value="Join" />
<input type="submit" id="createSub" class="btn btn-primary" value="Create" />
</div>
</form>
</div>
</div>
<div id="userFormArea" class="row">
<div class="col-md-12">
<form id="userForm">
<div class="form-group">
<label>Enter username</label>
<input class="form-control" id="username" />
</br>
<input type="submit" class="btn btn-primary" value="Login" />
</div>
</form>
</div>
</div>
<div class="row" id="messageArea">
<div class="col-md-4">
<div id="roomInfo" class="well">
<h3>Online users</h3>
<ul class="list-group" id="users"></ul>
</div>
</div>
<div class="col-md-8">
<div id="chat" style="overflow: scroll; height: 350px; background-color: rgb(245,245,245); padding: 10px; border: 1px solid rgb(221, 221, 221); border-radius: 5px;"></div>
<form id="messageForm">
<div class="form-group">
<label>Enter message</label>
<input type="text" id="message" class="form-control" />
</br>
<input type="submit" class="btn btn-primary" value="Send messsage" />
</div>
</form>
</div>
</div>
</div>
<script>
$(function(){
let socket = io.connect();
//first form (room number, if necessary)
let $roomFormArea = $('#roomFormArea');
let $roomForm = $('#roomForm');
let $room = $('#room');
//second form (usrename)
let $userFormArea = $('#userFormArea');
let $userForm = $('#userForm');
let $username = $('#username');
//third form (chat)
let $messageArea = $('#messageArea');
let $roomInfo = $('#roomInfo');
let $users = $('#users');
let $chat = $('#chat');
let $messageForm = $('#messageForm');
let $message = $('#message');
let $roomNumber = 0;
//ROOM SECTION =======================================================================
$('#createSub').click(function(e){
e.preventDefault();
socket.emit('new room', function(data){
if(data){
$roomFormArea.hide();
$userFormArea.show();
}
});
$room.val('');
});
$('#joinSub').click(function(e){
e.preventDefault();
if($room){
socket.emit('join room', $room.val(), function(data){
if(data){
$roomFormArea.hide();
$userFormArea.show();
}
else{
alert("The room number you entered doesn't exist. Try again.");
}
});
}
$room.val('');
});
//USER SECTION =======================================================================
$userForm.submit(function(e){
e.preventDefault();
socket.emit('new user', $username.val(), function(data){
if(data){
$userFormArea.hide();
$messageArea.show();
}
});
$username.val('');
});
//CHATROOM SECTION ===================================================================
socket.on('get room', function(data){
$roomInfo.prepend("<h2>Room number: " + data + "</h2>");
$roomNumber = data;
});
socket.on('get users', function(data){
let html = '';
for(let i = 0; i < data.length; i++){
html += '<li class="list-group-item">' + data[i] + '</li>';
}
$users.html(html);
});
//Message section ====================================
$messageForm.submit(function (e) {
e.preventDefault();
socket.emit('send message', $message.val(), $roomNumber);
$message.val('');
})
socket.on('new message', function (data) {
$chat.append('<strong>' + data.user + ':</strong> ' + data.msg + '</br>');
})
});
</script>
</body>
</html>