-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
311 lines (274 loc) · 8.71 KB
/
chat.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>online chatRoom</title>
<!-- <link href="chat.css" rel="stylesheet" type="text/css"> -->
<style>
html,body{
height: 100%;
}
body{
background-color:#f0f0f0;
margin:10px;
}
#wrapper{
position:relative;
width:100%;
height:46px;
background-color:black;
}
#item1{
position:relative;
margin:11px 10px;
padding-right:10px;
padding-left:50px;
border-right:2px white solid;
display:inline-block;
color:white;
}
#item2{
position:relative;
display:inline-block;
color:white;
}
#right{
position:relative;
margin:10px;
margin-left:30%;
width:100%;
height:90%;
}
#rightZone{
list-style-type: none;
width:100%;
height: 100%;
}
#chatZone{
width:70%;
height:80%;
list-style-type: none;
margin: 0;
padding: 0;
background-color:#EEE0E5;
word-wrap: break-word;
overflow-y:auto;
font-size: 20px;
font-weight: 500;
font-family: "HelveticaNeue-Light",
"Helvetica Neue Light",
"Helvetica Neue",
Helvetica,
Arial,
"Lucida Grande",
sans-serif;
}
#chatZone li { padding: 5px 10px; }
#inputZone{
margin-top:10px;
}
#shuru {
border: 0;
padding: 10px;
width: 50%;
margin-right: .5%;
}
#sendMessage{
width: 60px;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#left{
position:absolute;
left:10px;
top:66px;
width:28%;
height:70%;
}
#onlinePeople{
width:100%;
height:70%;
background-color:#EEE0E5;
}
#title{
padding:10px;
text-align:center;
color:red;
font-size: 22px;
font-weight: 500;
}
#list{
padding: 0px;
text-align: center;
font-size: 20px;
font-weight: 600;
color:black;
overflow-y: auto;
}
#inandout{
margin-top:10px;
text-align:center;
}
#info{
padding: 10px;
margin-bottom: 10px;
}
#getInRoom,#outOfRoom{
width: 35%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
margin: 5px;
}
</style>
</head>
<body>
<div id = "wrapper">
<div id = "item1">web版在线聊天室</div>
<div id = "item2">联系作者</div>
</div>
<div id= "right">
<li id = "rightZone">
<ul id = "chatZone"></ul>
<form id = "inputZone">
<input id = "shuru" placeholder="请输入聊天内容">
<button id = "sendMessage">发送</button>
</form>
</li>
</div>
<div id = "left">
<div id = "onlinePeople">
<div id = "title">在线人列表</div>
<ul id = "list"></ul>
</div>
<div id = "inandout">
<div id = "info"></div>
<input type="text" name="username" style="width:90%;height:20px;border: 0;
padding: 8px;margin-bottom: 10px" placeholder="请输入用户名" id = 'yonghu'>
<button id = "getInRoom" value="Login" onclick="login()">进入聊天室</button>
<button id = "outOfRoom" onclick="leaveRoom()">离开聊天室</button>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var username = null;
var socket = io.connect("http://localhost:8000",{"force new connection":true});
socket.on('connect',function () {
//console.log('connected to server');
$('#info').html('<span style="color:red;">ready to login</span>');
socket.on('loginSuccess',function (data) {
//console.log('login success');
username = data.username;
$('#info').html('<span style="color:red;">login success</span>');
refreshOnlineUsers(data.allusers);
});
socket.on('loginFailed',function(){
$('#info').html('<span style="color:red;">用户名不存在</span>');
$('#list').empty();
username = null;
});
socket.on('enterChat',function(data){
$('#chatZone').append($('<li>').text(data.username+'进入了聊天室'));
refreshOnlineUsers(data.allusers);
});
socket.on('leaveChat',function(data){
$('#chatZone').append($('<li>').text(data.username+'离开了聊天室'));
refreshOnlineUsers(data.allusers);
});
socket.on('chat message',function (data) {
var date = new Date( );
var h = date.getHours();
var hour = (h < 10 ? "0"+h : h) +":";
var m = date.getMinutes();
var min = (m < 10 ? "0"+m : m) +":";
var s = date.getSeconds();
var sec =(s < 10 ? "0"+s : s) + ' ';
var timeMsg =(data.username + ' ' + '[' + hour + min + sec + ']:'+ data.msg);
var item = $('#chatZone').append($('<li>').text(timeMsg));
});
});
function refreshOnlineUsers(data){
console.log(data);
if(username == null){
return;
}
$('#list').empty();
var item,i;
for(i=0;i<data.length;i++){
item = '<div style="padding-bottom: 3px">' +data[i]+ '</div>';
$('#list').append(item);
}
}
function leaveRoom(){
if(username === null){
return false;
}else {
socket.close()
$('input[name=username]').val('');
$('#info').html('<span style="color:red;">login out</span>');
$('#list').html('<li style="color:red;list-style: none;font-weight: 300;">已退出</li>' +
'<li style="color:red;list-style: none;font-weight: 300;">请登录查看</li>');
$('#chatZone').empty();
username = null;
$('#yonghu').fadeIn();
$('#getInRoom').fadeIn();
}
}
function login(){
var username = $('input[name=username]').val().trim().toLowerCase();;
if(username == ''){
alert('请输入用户名');
return;
} else{
//console.log({username:username});
$.ajax({
url:"http://localhost:8000/login",
type:"POST",
dataType:"json",
data:{username:username},
success:function (data) {
console.log(data)
if(data == 'true'){
socket.emit('login',{username:username});
$('#yonghu').hide();
$('#getInRoom').hide();
}
else if(data == 'false'){
$('#info').html('<span style="color:red;">user don\'t exist</span>');
$('#list').empty();
username = null;
}
},
error:function (ev) {
console.log(ev);
$('#info').html('<span style="color:red;">error!</span>');
}
})
}
}
$(function () {
$('#inputZone').submit(function () {
if(username == null){
alert('请登录!');
return false;
} else{
var msg = $('#shuru').val();
if(msg==null||msg.toString().trim()==''){
alert('内容不能为空');
return false;
} else {
socket.emit('chat message',{username:username,msg:msg});
$('#shuru').val('');
return false;
}
}
});
});
</script>
</body>
</html>