-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
134 lines (121 loc) · 4.42 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
<html>
<head>
<title>Chat with socket.io and node.js</title>
<style>
#chat{
height:500px;
}
#contentWrap{
display: none;
}
#chatWrap{
float: left;
border: 1px #000 solid;
}
.error{
color: red;
}
.whisper{
color: gray;
font-style: italic;
}
/* Inpuut fields */
.twitter {
width:250px;
border-radius:5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
background:#fff;
border:1px solid #ccc;
outline:none;
padding:6px;
}
.twitter:focus{
border:1px solid #56b4ef;
box-shadow:0px 0px 3px 1px #c8def0;
}
.avgrund-contents{
text-rendering:optimizelegibility;color:#0094ff;font-weight:bolder;text-shadow:0 1px 1px #CCC;position:relative;
display:inline-block;
}
</style>
</head>
<body>
<div class="avgrund-contents">
<header>
<h1> Chat Application <sup>{GSK}</sup></h1>
</div>
<div id="nickWrap">
<p>Enter a username:</p>
<p id="nickError"></p>
<form id="setNick">
<input type="text" name="text" class="twitter" size="35" id="nickname"></input>
<input type="submit"></input>
</form>
</div>
<div id="contentWrap">
<div id="chatWrap">
<div id="chat"></div>
<form id="send-message">
<input size="35" id="message"></input>
<input type="submit"></input>
</form>
</div> <b> Online Users </b>
<div id="users"></div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
jQuery(function ($) {
var socket = io.connect();
var $nickForm = $('#setNick');
var $nickError = $('#nickError');
var $nickBox = $('#nickname');
var $users = $('#users');
var $messageForm = $('#send-message');
var $messageBox = $('#message');
var $chat = $('#chat');
$nickForm.submit(function (e) {
e.preventDefault();
socket.emit('new user', $nickBox.val(), function (data) {
if (data) {
$('#nickWrap').hide();
$('#contentWrap').show();
} else {
$nickError.html('That username is already taken! Try again.');
}
});
$nickBox.val('');
});
socket.on('usernames', function (data) {
var html = '';
for (var i = 0; i < data.length; i++) {
html += data[i] + '<br/>'
}
$users.html(html);
});
$messageForm.submit(function (e) {
e.preventDefault();
socket.emit('send message', $messageBox.val(), function (data) {
$chat.append('<span class="error">' + data + "</span><br/>");
});
$messageBox.val('');
});
socket.on('load old msgs', function (docs) {
for (var i = docs.length - 1; i >= 0; i--) {
displayMsg(docs[i]);
}
});
socket.on('new message', function (data) {
displayMsg(data);
});
function displayMsg(data) {
$chat.append('<span class="msg"><b>' + data.nick + ': </b>' + data.msg + "</span><br/>");
}
socket.on('whisper', function (data) {
$chat.append('<span class="whisper"><b>' + data.nick + ': </b>' + data.msg + "</span><br/>");
});
});
</script>
</body>
</html>