-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest.html
134 lines (109 loc) · 3.29 KB
/
test.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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p><input id="btn1" type="button" value="connect normal" />    <input id="btn2" type="button" value="connect auth" /></p>
<p><textarea cols="100" rows="20" id="txt1"></textarea></p>
<script>
window.onload = initBody;
var ws;
function initBody(){
var oBtn1 = document.getElementById('btn1');
oBtn1.onclick = OnButton1;
var oBtn2 = document.getElementById('btn2');
oBtn2.onclick = OnButton2;
}
function initWebSocket(isauth) {
if (isauth){
//ws = new WebSocket('ws://192.168.8.178:8100/ws/onsocket?appid=10000&groupid=1&userid=' + newGuid());
ws = new WebSocket('ws://127.0.0.1:8100/ws/onauthsocket?appid=10000&groupid=1&userid=' + newGuid() + "&token=1");
}
else{
//ws = new WebSocket('ws://192.168.8.178:8100/ws/onsocket?appid=10000&groupid=1&userid=' + newGuid());
ws = new WebSocket('ws://127.0.0.1:8100/ws/onsocket?appid=10000&groupid=1&userid=' + newGuid());
//ws = new WebSocket('wss://192.168.8.178:8100/ws/onsocket?appid=10000&groupid=1&userid=' + newGuid());
}
ws.onopen = OnOpen;
ws.onmessage = OnMessage;
ws.onclose = OnClose;
ws.onerror = OnError;
}
function OnButton1() {
//send()
initWebSocket(false)
}
function OnButton2() {
//send()
initWebSocket(true)
}
var send = function() {
var data = function() {
ws.send("{'send':1,'take':2,'message':" + Math.random() + "}");
};
if (ws.readyState !== 1) {
ws.close();
initWebSocket();
setTimeout(function() {
data();
}, 1000);
} else {
data();
};
}
function OnOpen(event) {
var oTxt = document.getElementById('txt1');
ws.send("ws open");
oTxt.value =oTxt.value + "\r\nConnect Success";
oTxt.scrollTop = oTxt.scrollHeight;
}
function OnMessage(event) {
var oTxt = document.getElementById('txt1');
oTxt.value =oTxt.value + "\r\n" + event.data;
oTxt.scrollTop = oTxt.scrollHeight;
};
function OnClose(event){
var oTxt = document.getElementById('txt1');
oTxt.value =oTxt.value + "\r\n" + "connect close";
oTxt.scrollTop = oTxt.scrollHeight;
}
function OnError(event){
var oTxt = document.getElementById('txt1');
for ( var p in event) {
oTxt.value =oTxt.value + "\r\n" + "connect error[" + p+ "] =>" + event[p];
oTxt.scrollTop = oTxt.scrollHeight;
}
}
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
function newGuid()
{
var guid = "";
for (var i = 1; i <= 32; i++){
var n = Math.floor(Math.random()*16.0).toString(16);
guid += n;
if((i==8)||(i==12)||(i==16)||(i==20))
guid += "-";
}
return guid;
}
</script>
</body>
</html>