-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatroom.html
312 lines (298 loc) · 10.2 KB
/
chatroom.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
311
312
<!DOCTYPE html>
<html lang="cn">
<head>
<title>Talk2Me--Use HTML5 new features</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css">
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Talk2Me</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active">
<a href="#">
<i class="icon-home"></i>主页</a>
</li>
<li>
<a href="#about">关于</a>
</li>
<li>
<a href="#contact">联系我</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<h3 id="myModalLabel">新用户进入</h3>
</div>
<div class="modal-body">
<div id="nickname_control" class="control-group">
<div class="controls">
<input type="text" id="NickName" placeholder="请输入您的昵称" autofocus>
<span class="help-inline"></span>
</div>
</div>
<label class="radio">
爷们<input type="radio" name="options" value="b" checked>
</label>
<label class="radio">
妹子<input type="radio" name="options" value="g">
</label>
</div>
<div class="modal-footer">
<button class="btn btn-primary" id="userInfo" data-dismiss="modal" aria-hidden="true">进入</button>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<div class="well sidebar-nav">
<ul class="nav nav-list" id="user_list">
<li class="nav-header">Online People</li>
</ul>
</div>
<center><button class="btn btn-primary" id="quitChatroom">离开聊天室</button></center>
</div>
<div class="span9">
<fieldset class="well">
<p class="head text-success">
[+] 欢迎来到Talk2Me在线聊天室,本站基于HTML5多种新特性开发,Enjoy ur self!
</p>
<div class="text-info" id="chatlog" style="height:350px; overflow:auto;">
</div>
</fieldset>
<form class="form-inline">
<input type="text" id="messageTextBox" placeholder="输入聊天内容">
<button type="button" id="sendMessage" class="btn">发送</button>
</form>
</div>
</div>
<hr>
<footer>
<p>© pnig0s@Freebuf.Com 2012 Based on HTML5 new features</p>
</footer>
</div>
<script src="js/jquery-latest.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
var curr_user_nickname = '';
var curr_user_sex = '';
var socket;
var wsServer = 'ws://127.0.0.1:2012/chatServ'
$('#myModal').modal('show');
function checkAvailable ()
{
if(!("WebSocket" in window))
{
if(this.MozWebSocket)
{
WebSocket = this.MozWebSocket;
}else
{
return false;
}
}
return true;
}
var result = checkAvailable();
if (!result)
{
var message = "[-] 浏览器不支持或版本过低,无法使用Talk2Me。";
writeToChatLog(message,'text-error');
}else
{
socket =
{
start : function()
{
this._ws = new WebSocket(wsServer);
this._ws.onopen = this.whenOpen;
this._ws.onmessage = this.whenMessage;
this._ws.onclose = this.whenClose;
this._ws.onerror = this.whenError;
},
whenOpen : function(m)
{
var message = '[*] 成功连接聊天室服务器....';
writeToChatLog(message,'text-info');
},
sendMsg : function(message)
{
if (this._ws)
{
this._ws.send(message);
}
},
whenMessage : function(m)
{
var msgObj = JSON.parse(m.data);
if (msgObj.type == "JOIN")
{
writeToChatLog(msgObj.msg,msgObj.text_type);
if(!msgObj.succ)
{
return;
}
user = document.getElementById('user_list');
userNodes = document.getElementById('user_list').children;
userNodes_length = userNodes.length;
for(var i = 0;i<userNodes_length;i++)
{
if(userNodes[1])
{
user.removeChild(userNodes[1]);
}
}
for (var user_name in msgObj.user_list) {
user_list = document.getElementById("user_list");
var newUser = document.createElement("li");
var icon_type = "";
if(msgObj.user_list[user_name] == "b")
{
icon_type = "/img/boy.png";
}else
{
icon_type = "/img/girl.png";
}
if(user_name == curr_user_nickname)
{
newUser.innerHTML = '<img src='+icon_type+'/><strong> '+user_name+'</strong>';
}else
{
newUser.innerHTML = '<img src='+icon_type+'/> '+user_name;
}
user_list.appendChild(newUser);
}
}else if(msgObj.type == "QUIT")
{
user = document.getElementById('user_list');
userNodes = document.getElementById('user_list').children;
userNodes_length = userNodes.length;
for(var i = 0;i<userNodes_length;i++)
{
if(userNodes[1])
{
user.removeChild(userNodes[1]);
}
}
for (var user_name in msgObj.user_list) {
user_list = document.getElementById("user_list");
var newUser = document.createElement("li");
var icon_type = "";
if(msgObj.user_list[user_name] == "b")
{
icon_type = "/img/boy.png";
}else
{
icon_type = "/img/girl.png";
}
if(user_name == curr_user_nickname)
{
newUser.innerHTML = '<img src='+icon_type+'/><strong>'+user_name+'</strong>';
}else
{
newUser.innerHTML = '<img src='+icon_type+'/>'+user_name;
}
user_list.appendChild(newUser);
}
writeToChatLog(msgObj.msg,msgObj.text_type);
}else if (msgObj.type == "POST")
{
writeToChatLog(msgObj.msg,msgObj.text_type);
}
},
whenClose : function(m)
{
this._ws = null;
var message = '[-] 连接已断开.'
writeToChatLog(message,'text-error');
},
whenError : function(m)
{
var message = '[-] 出现未知错误,可能导致连接中断...';
writeToChatLog(message,'text-error');
}
};
}
function writeToChatLog (message,message_type)
{
document.getElementById('chatlog').innerHTML += '<p class=\"'+message_type+'\">'+message+'</p>';
}
window.onunload = function()
{
quit();
};
function quit () {
var msg = new Object;
msg.type = "QUIT";
msg.user_nickname = curr_user_nickname;
socket.sendMsg(JSON.stringify(msg));
}
socket.start();
document.getElementById('userInfo').addEventListener('click',function()
{
curr_user_nickname = document.getElementById('NickName').value;
var radio_list = document.getElementsByName('options');
for (var i=0;i<radio_list.length;i++)
{
if(radio_list[i].checked)
{
curr_user_sex = radio_list[i].value;
break;
}
}
var msg = new Object;
msg.type = "JOIN";
msg.user_nickname = curr_user_nickname;
msg.user_sex = curr_user_sex;
socket.sendMsg(JSON.stringify(msg));
setInterval(function()
{
var msg = new Object
msg.type = "HEARTBEAT";
socket.sendMsg(JSON.stringify(msg));
},60000);
},true);
document.getElementById('sendMessage').addEventListener('click',function()
{
message = document.getElementById('messageTextBox').value;
if (curr_user_nickname)
{
var msg = new Object;
msg.type = "POST";
msg.from = curr_user_nickname;
msg.context = message;
msg.to = '';
socket.sendMsg(JSON.stringify(msg));
}
},true);
document.getElementById('quitChatroom').addEventListener('click',function()
{
quit();
},true);
</script>
</body>
</html>