forked from archoncap/HappyChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
275 lines (241 loc) · 9.63 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>快乐聊天室</title>
<link rel="shortcut icon" href="favicon.ico">
<link href="static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="static/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
<link href="static/css/animate.min.css" rel="stylesheet">
<link href="static/css/style.min.css?v=4.1.0" rel="stylesheet">
<link href="static/layer/skin/default/layer.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<div class="ibox chat-view">
<div class="ibox-title">
<a href="javascript:loginOut();" class="roll-nav roll-right J_tabExit" style="float: right;height: 20px"><i class="fa fa fa-sign-out"></i> 退出</a>
<span id="tips">聊天窗口</span>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-md-9 ">
<div class="chat-discussion" id="chatbox" style="height: 650px">
</div>
</div>
<div class="col-md-3">
<div class="chat-users">
<div class="users-list">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="chat-message-form">
<div class="form-group">
<textarea class="form-control message-input" name="message" placeholder="输入消息内容,按回车键发送" id="message"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- login box -->
<div class="ibox-content" style="display: none;width:350px;height:150px" id="loginBox">
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">用户名:</label>
<div class="col-sm-8">
<input type="text" placeholder="用户名" class="form-control" id="uname">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-8">
<button class="btn btn-sm btn-primary m-t-n-xs" type="button" id="lgbtn">登 录</button>
</div>
</div>
</form>
</div>
<script src="static/js/jquery.min.js?v=2.1.4"></script>
<script src="static/js/bootstrap.min.js?v=3.3.6"></script>
<script src="static/layer/layer.js"></script>
<script type="text/javascript">
var logbox;
$(function(){
//建立socket连接
startConnect();
//check login
checkLogin();
//send message
document.getElementById('message').addEventListener('keydown',function(e){
if(e.keyCode!=13) return;
e.preventDefault(); //取消事件的默认动作
sendMessage();
this.value = '';
});
//login
$("#lgbtn").click(function(){
if( $("#uname").val() == '' ){
layer.alert('用户名不能为空', {"title":"友情提示", "icon":2});
return false;
}
doLogin( $("#uname").val() );
layer.close( logbox );
});
});
//检查登录
function checkLogin(){
var user_info = localStorage.getItem("userInfo");
if( !user_info ){
showLogin();
}else{
user_info = $.parseJSON( user_info );
var msg = '{"type" : "1", "user" : "' + user_info.uname + '", "avar" : "' + user_info.avar + '"}';
if(ws.readyState ===1){
ws.send(msg);
}else{
setTimeout("ws.send( '"+msg+"' )", 1000);//如果发现连接被占用,就暂时停1秒钟
}
}
return user_info;
}
//登录操作
function showLogin(){
logbox = layer.open({
type: 1,
title:'欢迎加入快乐聊天室',
skin: 'layui-layer-demo', //加上边框
closeBtn: 0, //不显示关闭按钮
area: ['380px', '200px'], //宽高
content: $("#loginBox")
});
}
//简历socket连接
function startConnect(){
// 创建一个Socket实例
ws = new WebSocket('ws://127.0.0.1:8090');
var loading = layer.load(0, {shade: false}); //0代表加载的风格,支持0-2
ws.onopen = function(){
console.log("握手成功");
layer.close(loading);
};
ws.onmessage = function(e){
var data = $.parseJSON( e.data );
if( data.type == 2 ){
tellOnline( data.user );
}
if( data.type == 3 ){
var message = parseMessage( data.user, data.stime, data.avar, data.msg );
$("#chatbox").append( message );
var ex = document.getElementById("chatbox");
ex.scrollTop = ex.scrollHeight;
}
if( data.type == 5 ){
//console.log( data );
var user_list = showOnlineUser( data.info );
$(".users-list").html( user_list );
}
if( data.type == 6 ){
tellOutline( data.user );
}
};
ws.onerror = function(){
layer.close( loading );
layer.msg( "登录失败", {'time' : 1000});
};
}
function doLogin( name ){
//获取头像
var avar = parseInt(Math.random() * 10);
if( avar == 0 ) avar = 1;
avar = 'a' + avar + '.jpg';
localStorage.setItem('userInfo', '{"uname" : "' + name + '", "avar" : "' + avar + '"}');
$("#tips").text('您好:' + name);
var msg = '{"type" : "1", "user" : "' + name + '", "avar" : "' + avar + '"}';
ws.send( msg );
}
//退出登录
function loginOut(){
var msg = '{"type" : "4", "msg" : "login out"}';
ws.send( msg );
localStorage.setItem('userInfo', '');
layer.load(0, {shade: false}); //0代表加载的风格,支持0-2
setTimeout( function(){
window.location.reload();
}, 2);
}
//提示上线
function tellOnline( name ){
layer.msg( name + '上线了', {time : 3000});
}
//提示下线
function tellOutline( name ){
layer.msg( name + '下线了', {time : 1000});
}
//发送消息
function sendMessage(){
//format date
Date.prototype.format =function(format)
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4- RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var times = new Date().format("yyyy-MM-dd hh:mm:ss");
var userinfo = checkLogin();
if( userinfo ){
//socket send
var msg = '{"type" : "3", "user" : "' + userinfo.uname + '", "avar" : "' + userinfo.avar + '", "stime" : "'
+ times + '", "msg": "' + $("#message").val() + '"}';
// console.log(msg);
ws.send( msg );
var message = parseMessage( userinfo.uname, times, userinfo.avar, $("#message").val() );
$(".chat-discussion").append( message );
$("#message").val('');
//滚动条自动定位到最底端
var ex = document.getElementById("chatbox");
ex.scrollTop = ex.scrollHeight;
}
}
//解析消息发送样式
function parseMessage( user, time, avar, message ){
var _html = '<div class="chat-message"><img class="message-avatar" src="static/img/' + avar + '" alt="">';
_html += '<div class="message"><a class="message-author" href="#"> ' + user + '</a>';
_html += '<span class="message-date"> ' + time + ' </span>';
_html += '<span class="message-content">' + message + '</span></div></div>';
return _html;
}
//展示在线人员
function showOnlineUser( info ){
var _html = '';
$.each( info, function(k, v){
_html += '<div class="chat-user"><span class="pull-right label label-primary">在线</span>';
_html += '<img class="chat-avatar" src="static/img/' + v.avar + '" alt=""><div class="chat-user-name">';
_html += '<a href="#">' + v.user + '</a></div></div>';
});
return _html;
}
</script>
</body>
</html>