-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
95 lines (93 loc) · 3.7 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
<html>
<head>
<title>Tech Blog</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style>
body{
margin-top: 30px;
}
#messageArea{
display:none
}
</style>
</head>
<body>
<div class="conatiner">
<div id="userFormArea">
<div class="col-md-12">
<form id="userForm">
<div class="form-group">
<label>Enter User Name</label>
<textarea class="form-control" id="username"></textarea>
</div>
<input type="submit" class="btn btn-primary" value="login"/>
</form>
</div>
</div>
<div id="messageArea" class="row">
<div>
<div class="col-md-4">
<div class="well">
<h3>Online User</h3>
<ul class="list-group" id="users"></ul>
</div>
</div>
</div>
<div>
<div class="col-md-8">
<div class="chat" id="chat"></div>
<form id="messageForm">
<div class="form-group">
<label>Enter User Name</label>
<textarea class="form-control" id="message"></textarea>
</div>
<input type="submit" class="btn btn-primary" value="Send Message"/>
</form>
</div>
</div>
</div>
</div>
<script>
$(function () {
var socket = io.connect();
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
var $messageArea = $('#messageArea');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $users = $('#users');
var $username = $('#username');
$messageForm.submit(function (e) {
e.preventDefault();
socket.emit('send message', $message.val());
$message.val(' ');
});
socket.on('new message', function (data) {
$chat.append('<div class="well"><strong>' + data.user + '</strong> :' + data.msg + '</div>')
});
$userForm.submit(function (e) {
console.log('login user');
e.preventDefault();
socket.emit('new user', $username.val(), function (data) {
if (data) {
$userFormArea.hide();
$messageArea.show();
}
});
$username.val(' ');
});
socket.on('get users', function (data) {
console.log('get users=',data);
var html = '';
for (i = 0; i < data.length; i++) {
html += '<li class="list-group-item">' + data[i] + '</li>';
}
$users.html(html);
});
});
</script>
</body>
</html>