-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
346 lines (302 loc) · 9.58 KB
/
index.html
File metadata and controls
346 lines (302 loc) · 9.58 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!--
Nodejs实现匿名聊天群,欢迎开车
https://github.com/kylescript/nodejs-chat
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta charset="UTF-8">
<title>匿名开车群</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/js-cookie/latest/js.cookie.min.js"></script>
<style>
body {
font-size: 14px;
}
.init {
text-align: center;
}
.current-user {
text-align: center;
margin: 10px;
}
.change-name {
color: dodgerblue;
cursor: pointer;
font-size: 14px;
}
.content {
display: none;
}
.chat {
margin: 0 8px;
background-color: #fcfcfc;
border: dimgray 1px solid;
border-radius: 5px;
height: 400px;
overflow: scroll;
overflow-x: hidden;
overflow-y: visible;
}
.cell-left {
margin: 6px 0;
}
.cell-right {
margin: 6px 0;
text-align: right;
}
.cell-left span {
font-size: 12px;
color: chocolate;
font-style: italic;
}
.cell-right span {
color: lime;
font-size: 12px;
font-style: italic;
float: right;
margin-right: 5px;
}
.cell-left p {
margin: 5px 0 5px 10px;
padding: 5px;
display: inline-block;
color: #101010;
background-color: #e6e5ed;
border-radius: 8px;
max-width: 75%;
word-break: break-all;
word-wrap: break-word;
}
.cell-right p {
margin: 5px 10px 5px 0;
padding: 5px;
display: inline-block;
color: #FAFAFA;
background-color: #1E8EFE;
border-radius: 8px;
max-width: 75%;
text-align: left;
word-break: break-all;
word-wrap: break-word;
}
.chat-send {
margin: 10px 8px;
}
input {
display: inline-block;
border: #ccc 1px solid;
border-radius: 3px;
padding: 8px 5px;
width: 75%;
}
button {
width: 15%;
padding: 8px 0;
margin-left: 4px;
background-color: darkgreen;
border-radius: 3px;
border: #ccc 1px solid;
font-size: 12px;
color: white;
}
/* start 初始化动画 start */
#loading3 {
left: 50%;
margin: 20px 0 0 -25px;
position: relative;
width: 50px;
height: 50px;
}
.demo3 {
width: 4px;
height: 4px;
border-radius: 2px;
background: black;
position: absolute;
animation: demo3 linear 0.8s infinite;
-webkit-animation: demo3 linear 0.8s infinite;
}
.demo3:nth-child(1) {
left: 24px;
top: 2px;
animation-delay: 0s;
}
.demo3:nth-child(2) {
left: 40px;
top: 8px;
animation-delay: 0.1s;
}
.demo3:nth-child(3) {
left: 47px;
top: 24px;
animation-delay: 0.1s;
}
.demo3:nth-child(4) {
left: 40px;
top: 40px;
animation-delay: 0.2s;
}
.demo3:nth-child(5) {
left: 24px;
top: 47px;
animation-delay: 0.4s;
}
.demo3:nth-child(6) {
left: 8px;
top: 40px;
animation-delay: 0.5s;
}
.demo3:nth-child(7) {
left: 2px;
top: 24px;
animation-delay: 0.6s;
}
.demo3:nth-child(8) {
left: 8px;
top: 8px;
animation-delay: 0.7s;
}
@keyframes demo3 {
0%, 40%, 100% {
transform: scale(1);
}
20% {
transform: scale(3);
}
}
@-webkit-keyframes demo3 {
0%, 40%, 100% {
transform: scale(1);
}
20% {
transform: scale(3);
}
}
/* end 初始化动画 end */
</style>
</head>
<body>
<div class="init">正在初始化,请稍等...
<div id="loading3">
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
<div class="demo3"></div>
</div>
</div>
<div class="content">
<div class="current-user">当前用户: <a class="user-name">AnyOne</a><a class="change-name"> (换个名字)</a></div>
<div class="chat">
<!--<div class="cell-left">-->
<!--<p><span>黄旭东</span><br>MD老子是专业解说好吗?这怎么奶死嘛,专业解说看这种局面还看不懂啊?</p>-->
<!--</div>-->
<!--<div class="cell-right">-->
<!--<p><span>鲁迅</span><br>我即使是死了,钉在在棺材里了,也要在墓里,用这腐朽的声带喊出:“我没说过这句话。”</p>-->
<!--</div>-->
</div>
<div class="chat-send">
<input class="send-content" type="text" placeholder="请输入内容(回车发送)...">
<button class="send-btn">发送</button>
</div>
</div>
<script>
// 我的名字
let name = "AnyOne";
let cookie_name = 'cookie_name';
// 定义消息前缀,表示特定的含义
const prefix_new_id = '[ID_NEW]'; // 分配名字给新用户
const prefix_change_id = '[ID_CHANGE]'; // 老用户换名字
const prefix_use_id = '[ID_USE]'; // 老用户用cookie里的老名字
const prefix_message_text = '[MESSAGE_TEXT]'; // 文本消息
const prefix_message_photo = '[MESSAGE_PHOTO]'; // 图片消息
// 文本消息名字、内容分隔符
const split_str = "#9527#";
function create_chat_cell(user_name, message) {
function html2Escape(sHtml) {
return sHtml.replace(/[<>&"]/g, (c) => {
return {'<': '<', '>': '>', '&': '&', '"': '"'}[c];
});
}
// 暂时去掉吧,让大家玩。
// user_name = html2Escape(user_name);
// message=html2Escape(message);
// 自己的消息,放在右侧
if (name === user_name) {
return '<div class="cell-right"><p><span>' + user_name + '</span><br>' + message + '</p></div>'
}
return '<div class="cell-left"><p><span>' + user_name + '</span><br>' + message + '</p></div>'
}
// document ready
$(() => {
let ws = new WebSocket('ws://68.168.141.229:9527');
ws.onopen = () => {
console.log('client connect server successfully.');
$('.init').css('display', 'none');
$('.content').css('display', 'block');
let old_name = Cookies.get(cookie_name);
if (old_name === undefined) {
ws.send(prefix_new_id);
} else {
ws.send(prefix_use_id + old_name);
}
};
// 连接断开了
ws.onerror = () => {
alert("Σ( ° △ °|||)︴ 服务器被二师兄吃掉了!!!");
location.reload();
};
ws.onmessage = message => {
let m = message.data;
console.log('Recv:' + m);
if (m.startsWith(prefix_new_id)) { // 新名字
let new_name = m.substring(prefix_new_id.length);
Cookies.set(cookie_name, new_name, {expires: 3});
$('.user-name').text(new_name);
name = new_name;
} else if (m.startsWith(prefix_message_text)) {
let text = m.substring(prefix_message_text.length);
let pos = text.indexOf(split_str);
let user_name = text.substring(0, pos);
let message = text.substring(pos + split_str.length);
console.log(user_name + ":" + message);
let cell = create_chat_cell(user_name, message);
$('.chat').append(cell);
// 延迟0.2秒滚动到底部
setTimeout(() => {
// 这两种写法都可以,但是貌似第二种更专业一点吧
// $('.chat')[0].scrollTop = $('.chat')[0].scrollHeight;
$('.chat').prop('scrollTop', $('.chat').prop('scrollHeight'));
}, 100);
}
};
$('.change-name').click(() => {
ws.send(prefix_change_id);
});
$('.send-btn').click(() => {
let text = $('.send-content').val();
if (text === '') {
alert("不能发送空白信息!");
return;
}
ws.send(prefix_message_text + text);
$('.send-content').val("");
});
});
// 响应回车登录事件
document.onkeydown = function (e) {
if (!e) e = window.event;
if (e.key === 'Enter') {
e.preventDefault();
$('.send-btn').click();
}
};
</script>
</body>
</html>