-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.html
108 lines (91 loc) · 2.82 KB
/
demo.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<style type="text/css" media="screen">
body{background-color: rgb(12,16,33);font-size: 14px;font-family: '微软雅黑';}
#msglist{width: 100%;overflow-x: hidden;overflow-y:auto;}
#msglist p{margin: 0;}
.user{color:rgb(255,100,0);}
.time{margin-left: 20px;color:rgb(251,222,45);}
.msg{color:white;}
#helper{width: 100%;height: 25px;position: fixed;bottom:25px;line-height: 25px;color:rgb(150,174,130);}
#userinput{width: 100%;height: 25px;position: fixed;bottom:0px;overflow: hidden;line-height: 25px;}
textarea{resize: none;width: 100%;height: 25px;line-height: 25px;}
</style>
<title>websocket demo</title>
</head>
<body>
<div id="msglist"></div>
<div id="helper">
Send[Enter]/Clear[Alt+C]
</div>
<div id="userinput">
<textarea class="content" id="content"></textarea>
</div>
<script type="text/html" id="tpl">
<p><span class="user">{ip}</span><span class="time">{date}</span></p>
<p class="msg">{msg}</p>
</script>
<script>
var App = function(){
function sub(str, data) {
return str.replace(/{(.*?)}/igm, function($, $1) {
return data[$1] ? data[$1] : $;
});
}
function fixHeight(content){
var cHeight= document.body.clientHeight || document.documentElement.clientHeight
var nHeight = (cHeight - 55) + 'px';
content.style.height = nHeight;
}
function getTime(){
var curTime = new Date();
var strTime = curTime.getFullYear() + '/';
strTime += (curTime.getMonth() + 1) + '/';
strTime += curTime.getDate() + ' ';
strTime += curTime.getHours() + ':';
strTime += curTime.getMinutes() + ':';
strTime += curTime.getSeconds();
return strTime;
}
return function(){
var ws = new WebSocket("ws://172.18.211.20:8000");
var tpl = document.querySelector('#tpl').innerHTML;
var content = document.querySelector('#msglist');
var eInput = document.querySelector('#content');
fixHeight(content);
ws.onmessage = function(e){
var data = JSON.parse(e.data);
data.date = getTime();
var html = sub(tpl,data);
content.innerHTML += html;
content.scrollTop = content.scrollHeight;
};
document.addEventListener('keyup',function(event){
var keyCode = event.keyCode;
if(keyCode == 13){
var msg = eInput.value;
msg = msg.replace(/(^\s*)|(\s*$)/g, "");
if(msg){
ws.send('text' + msg);
}
eInput.value = '';
}
},false);
document.addEventListener('keydown',function(event){
var keyCode = event.keyCode;
if(event.altKey && keyCode == 67){
content.innerHTML = '';
}
},false);
ws.onclose = function(e){
alert('server closed');
};
}
}();
window.addEventListener('DOMContentLoaded', App, false);
</script>
</body>
</html>