-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowLog.html
109 lines (97 loc) · 4.01 KB
/
windowLog.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
<!DOCTYPE html>
</html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id = 'clientid' style="display: none">1</div>
<div id="login">
<div>
<input style="float: left" id="serverIP" type="text" placeholder="服务器IP" value="127.0.0.1" autofocus="autofocus" />
<input style="float: left" id="serverPort" type="text" placeholder="服务器端口" value="9000" />
<input style="float: left" id="btnConnect" type="button" value="连接" onclick="connect()" />
</div>
<div>
<input id="sendText" type="text" placeholder="发送文本" value="log path" />
<input id="btnSend" type="button" value="发送" onclick="send()" />
<input id="btnClear" type="button" value="清屏" onclick="clearAll()" />
</div>
<div>
<!--<div>-->
<!--来自服务端的消息-->
<!--</div>-->
<textarea style="background-color: #0c0c0c ; width: 100% ; height: 100%; color: lawngreen" id="txtContent" cols="200" rows="40" readonly="readonly"></textarea>
</div>
</div>
</body>
<script>
var socket;
function connect() {
var host = "ws://" + $("serverIP").value + ":" + $("serverPort").value + "/"
socket = new WebSocket(host);
try {
socket.onopen = function (msg) {
$("btnConnect").disabled = true;
alert("连接成功!");
};
socket.onmessage = function (msg) {
if (typeof msg.data == "string") {
displayContent(msg.data);
}
else {
alert("非文本消息");
}
};
socket.onclose = function (msg) {alert("socket closed!"); };
}
catch (ex) {
log(ex);
}
}
function send() {
var info = document.getElementById('clientid').innerHTML;
var msg = $("sendText").value
socket.send(info + ',' + msg);
}
window.onbeforeunload = function () {
try {
socket.close();
socket = null;
}
catch (ex) {
}
};
function $(id) { return document.getElementById(id); }
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
function displayContent(msg) {
if (msg.substring(0,4)=="{[}]"){
$("txtContent").value += msg.replace('{[}]' , '');
}else if(msg == '{[test}]test info'){
}
else{
if(document.getElementById('clientid').innerHTML != msg.split(",")[0]){
document.getElementById('clientid').innerHTML = msg.split(",")[0];
}
$("txtContent").value += "\r\n" +new Date().Format("yyyy/MM/dd hh:mm:ss")+ ": " + msg.replace(msg.split(",")[0]+',' , '');
}
}
function onkey(event) { if (event.keyCode == 13) { send(); } }
function clearAll(){
$("txtContent").value = '';
}
</script>
</html>