-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
291 lines (267 loc) · 6.58 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>匿名聊天</title>
<link href="bootstrap.css" rel="stylesheet">
<style>
body {
padding:1%;
}
#console {
height: 400px;
overflow: auto;
width:80%;
float:left;
}
#userlist{
height: 400px;
float :right;
width:15%;
background-color:#F5F5F5;
}
#userlist ul li{
border: solid 1px #9EDFF5;
-moz-border-radius: 5px;
-webkit-border-radius:5px;
border-radius:5px;
}
.atl{
text-align:left;
}
.atr{
text-align:left;
}
.rowf{
float:left;
margin-top:10px;
margin-left:5px;
padding:2px;
min-height:40px;
width:40%;
border: solid 1px #9EDFF5;
-moz-border-radius: 5px;
-webkit-border-radius:5px;
border-radius:5px;
word-break: break-all;word-wrap: break-word;
overflow;hidden;
}
.rowr{
float:right;
margin-top:10px;
margin-right:5px;
padding:2px;
min-height:40px;
width:40%;
border: solid 1px #FF0000;
-moz-border-radius: 5px;
-webkit-border-radius:5px;
border-radius:5px;
word-break: break-all;word-wrap: break-word;
overflow;hidden;
}
.clear{
clear:both;
}
.username-msg {color:orange;}
.connect-msg {color:green;}
.disconnect-msg {color:red;}
.send-msg {color:#888}
.sign_red{
width:15px; height:15px; line-height:14px;
border-radius: 90px;
background:red;
cursor:pointer;
}
.sign_green{
width:15px; height:15px; line-height:14px;
border-radius: 90px;
background:green;
cursor:pointer;
}
</style>
</head>
<body>
<h1>匿名聊天初版 V_0.1</h1>
<br/>
<div id="console" class="well">
</div>
<div id="userlist">
<ul>当前在线亻</ul>
<ul id="usershow">
</ul>
</div>
<div style="clear:both;">
<form class="well form-inline" onsubmit="return false;">
<input id="msg" class="input-xxlarge" type="text" placeholder="输入聊天内容..."/>
<button type="button" id="sendmsg" class="btn">发送消息</button>
<button type="button" id="discon" class="btn">断开连接</button>
<!--<button type="button" id="bsl" title="私聊状态下,可以切换进入公聊" class="btn">信号切换</button>-->
<span id="sign" class="sign_green" title="信号灯绿色表示公聊,红色表示私聊"> </span>
</form>
<div>
<div style="color:blue;display:inline;font-size:12px;">
点击右边用户,进入私聊</br>
基于websocket技术,支持浏览器:chrome,firefox,360极速等,不支持IE</br>
后期可支持发送文件。</br>
可支持嵌入手机端应用
</div>
</body>
<script src="js/socket.io/socket.io-1.3.7.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/jquery-1.10.1.min.js"></script>
<script>
$(function(){
var userName = 'user' + Math.floor((Math.random()*1000)+1);
$(document).attr("title","我是"+userName);
//会话id
var sessionid = "";
//私聊id
var slid = "";
var socket = io.connect('http://172.22.0.7:9092');
/**
* 发送消息绑定回车
**/
$("#msg").keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
sendMessage();
}
});
/**
* 发送消息
**/
$("#sendmsg").click(function(){
sendMessage();
});
/**
* 端开连接
**/
$("#discon").click(function(){
sendDisconnect();
});
/**
* 点击切换进入公聊
**/
$("#sign").click(function(){
slid = "";
signfun("sign_green");
});
/**
* 点击用户列表,进入私聊
**/
$("#usershow").on("click","a",function(){
var id = $(this).attr("pid");
slid = id;
signfun("sign_red");
var username = $(this).find("li").html();
output('<span class="disconnect-msg">你将发送消息给 '+username+'</span>');
});
/**
* 信号切换
**/
function signfun(sign){
$("#sign").attr("class",sign);
}
/**
* 建立连接时,消息回调
**/
socket.on('connect', function() {
output('<span class="connect-msg">'+userName+' 进入匿名聊天!</span>');
send('user:join',{userName:userName});
});
/**
* 普通聊天
**/
socket.on('user:chat', function(data) {
if(data.userName!=userName){
output('<span class="username-msg">' + data.userName + ':</span> ' + data.message,"atr");
}else{
output('<span class="username-msg">' + data.userName + ':</span> ' + data.message);
}
});
/**
* 用户登录后,返回会话id
**/
socket.on('user:join', function(data) {
sessionid = data.id;
});
/**
* 端开连接回调
**/
socket.on('disconnect', function() {
output('<span class="disconnect-msg">断开连接!</span>');
});
/**
* 私聊消息回调
**/
socket.on('user:chatone', function(data) {
output('<span class="disconnect-msg">' + data.userName + ': 对你说 '+data.message+'</span> ','atr');
});
/**
* 发送消息
**/
function send(msgType,msgdata){
socket.emit(msgType, msgdata);
}
/**
* 获取用户列表,目前为定时,之后会改为后台推送
**/
setInterval(function(){
sysUserList();
},3000);
/**
* 获取用户列表回调
**/
socket.on('user:list', function(data) {
$("#usershow").html("");
$(data).each(function(index,item){
if(userName!=item.userName)
$("#usershow").append("<a href='#' pid="+item.id+"><li>"+item.userName+"</li></a>")
});
});
/**
* 发送获取用户列表
**/
function sysUserList(){
send("user:list",{})
}
/**
* 端开连接
**/
function sendDisconnect() {
socket.disconnect();
}
/**
* 发送消息
**/
function sendMessage() {
var message = $('#msg').val();
$('#msg').val('');
if(slid!=""){
var jsonObject = {userName: userName, message: message,slid:slid};
send('user:chatone',jsonObject);
}else{
var jsonObject = {userName: userName, message: message};
send('user:chat',jsonObject);
}
}
/**
* 展示消息
**/
function output(message,clas) {
if(!clas){
clas = "atl rowf";
}else{
clas = clas+" rowr"
}
var currentTime = "<span class='time'>" + moment().format('HH:mm:ss') + "</span>";
var element = $("<div class='row "+clas+"'>" + currentTime + " " + message + "</div>");
$('#console').append(element);
var clear = $("<div class='clear'></div>");
$('#console').append(clear);
$('#console').scrollTop( $('#console')[0].scrollHeight );
}
});
</script>
</html>