-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
172 lines (141 loc) · 5.59 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>
<head>
<meta charset="utf-8">
<title>Chat </title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script src="animate.js"></script>
</head>
<body>
<div id="container">
<div id='namesWrapper'>
<!-- <form id='usernameForm'>
<input size='40' id='username' placeholder="choose a username">
<input type="submit" value="Submit">
</form> -->
<div class="form" id='usernameForm'>
<ul class="tab-group">
<li class="tab active"><a href="#signup">Sign Up</a></li>
<li class="tab"><a href="#about">About this Site</a></li>
</ul>
<div class="tab-content">
<div id="signup">
<h1>Sign Up for Node.js Chat</h1>
<form>
<div class="field-wrap">
<label>
Pick a Username<span class="req">*</span>
</label>
<input id='username' required autocomplete="off"/>
</div>
<button type="submit" value="Submit" class="button button-block"/>Get Started</button>
</form>
</div>
<div id="about">
<h1>A Real-Time Chat Application</h1>
<form action="/" method="post">
<div class="field-wrap">
<ul id="example4">
<h3>Server Side </h3>
<li>Node.js</li>
<li>Socket.io</li>
<li>Express</li>
</ul>
</div>
<div class="field-wrap">
<ul id="example4">
<h3>Client Side </h3>
<li>HTML</li>
<li>jQuery</li>
<li>CSS3</li>
</ul>
</div>
<h3>Get the code</h3>
<h4>https://github.com/saurabh2816/nodejs-chat</h4>
<h3>Run</h3>
<h4>node app.js</h4>
<h4>run localhost:8000</h4>
<p class="forgot"><a href="http://saurabhrana.me">Visit saurabhrana.me</a></p>
</form>
</div>
</div><!-- tab-content -->
</div> <!-- /form -->
</div> <!--namewrapper end-->
<div id="error"></div>
<div id='mainWrapper'>
<h1 style="font-family: 'Press Start 2P', cursive; margin: 25px 0 20px 0px; text-align: left;">Nodejs Chat App</h1>
<div id="chatWrapper">
<div id="chatWindow">
</div>
<form id='messageForm'>
<input size='40' type="text" id='message' placeholder="type something..." style="width: 415px;" autocomplete="off">
<input type="submit" value="Send" class="button button-block">
</form>
</div>
<div id="userWrapper">
<div id="usersonline"></div>
<div id="users"></div>
</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<!-- this socket.io.js file doesn't actually exists. This will actually be served up by socket.io because when
we give them a http server to listen on , they basically create a route which listens for a GET request to this url.
And they'll server up some javascript which will be used to connect to the server
-->
<script>
$(function() {
var socket = io.connect(); // 'io' is created in global window scope when we rquire the socket.io/socket.io.js file
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chatWindow');
var $usernameForm = $('#usernameForm');
// var $users = $('#users');
var users = document.getElementById('users');
var $username = $('#username');
var $usersonline = $('#usersonline');
var $error = $('#error');
$usernameForm.submit(function(e) {
e.preventDefault();
socket.emit('new user', $username.val(), function(data, returnedUsername) {
if(data) {
$('#namesWrapper').hide();
$('#error').hide();
$('#mainWrapper').show();
} else {
if(returnedUsername.toString() === "")
{
$error.html('Empty string not an acceptable username');
$('#error').show();
}
else {
$error.html('Ooops! Looks like ' +'<b>' + returnedUsername + '</b>' + ' is already taken!');
$('#error').show();
}
}
});
$username.val('');
});
socket.on('usernames', function(data) {
var html = "";
for(i=0; i<data.length; i++)
html += '<font color="red">'+data[i] + '</font><br>';
users.innerHTML = html;
usersonline.innerHTML = "<h3> " + data.length + " users online </h3>";
});
$messageForm.submit(function(e) {
e.preventDefault(); //If this method is called, the default action of the event will not be triggered
socket.emit('send message', $message.val());
$message.val(''); //after message is shown, clear the message
});
// if there is a new message at the server (sent by any other user) append it to the chatarea
socket.on('new message', function(data) {
$chat.append('<div class="new_message">' + '' + data.user + ' : '+ data.msg + '</div>' +'<br>');
});
});
</script>
</body>
</html>