-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
111 lines (101 loc) · 3.08 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
<!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">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
body{
margin-top: 30px;
}
#messageFormArea{
display: none;
}
</style>
<title>IO Chat</title>
</head>
<body>
<div class="container">
<div id="userFormArea" class="row">
<div class="col-md-12">
<form id="userForm">
<div class="form-group">
<label for="username">Enter Username</label>
<input name="username" class="form-control" id="username">
<br>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</form>
</div>
</div>
<div id="messageFormArea" class="row">
<div class="col-md-4">
<div class="well">
<h3>Online Users</h3>
<ul class="list-group" id="users"></ul>
</div>
</div>
<div class="col-md-8">
<div class="chat" id="chat"></div>
<form id="messageForm">
<div class="form-group">
<label for="message">Enter Message</label>
<textarea name="message" class="form-control" id="message"></textarea>
<br>
<input type="submit" class="btn btn-primary" value="Send Message">
</div>
</form>
</div>
</div>
</div>
<script>
$(function(){
var socket = io.connect();
var $messageFormArea = $('#messageFormArea');
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $username = $('#username');
var $users = $('#users');
$messageForm.submit((e) => {
e.preventDefault();
socket.emit('send message', $message.val());
$message.val('');
});
socket.on('new message', (data) => {
$chat.append('<div class="well">'+'<strong>'+data.user+': </strong>'+data.msg+'</div>');
});
$userForm.submit((e) =>{
e.preventDefault();
socket.emit('new user', $username.val(), (data) => {
if(data){
$userFormArea.hide();
$messageFormArea.show();
}
});
$username.val('');
});
socket.on('get users', (data) => {
var html = '';
for(let i = 0; i < data.length; i++){
html += '<li class="list-group-item">'+data[i]+'</li>';
}
$users.html(html);
});
socket.on('get messages', (data) =>{
var chat2 = '';
for(let i = 0; i < data.length; i++){
//$chat.append('<div class="well">'+'<strong>'+data[i].user+': </strong>'+data[i].msg+'</div>');
chat2 += '<div class="well">'+'<strong>'+data[i].user+': </strong>'+data[i].msg+'</div>';
}
$chat.html(chat2);
});
});
</script>
</body>
</html>