-
Notifications
You must be signed in to change notification settings - Fork 6
/
connector.html
122 lines (103 loc) · 2.8 KB
/
connector.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
<html>
<style>
body {
background-color: gray;
font-size: 8px;
color: white;
font-weight: bold;
overflow: hidden;
font-family: "Lato", "proxima-nova", "Helvetica Neue", Arial, sans-serif;
}
</style>
<body>
</body>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:9001/ws";
var output;
var dobutton;
var sentMessages = [];
var totalSent = 0;
var run = true;
var MSG_TARGET = 50;
var stillSending = true;
var websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
onOpen(evt)
};
websocket.onclose = function (evt) {
onClose(evt)
};
websocket.onmessage = function (evt) {
onMessage(evt)
};
websocket.onerror = function (evt) {
onError(evt)
};
function onOpen(evt) {
writeToScreen("CONNECTED");
}
function onClose(event) {
if (event.code == 1000) {
writeToScreen("CONN CLOSED");
changeBG('green');
}
else {
writeToScreen(event.code);
changeBG('red');
}
}
function onMessage(event) {
var msg = event.data;
if (sentMessages.indexOf(msg) > -1) {
sentMessages.splice(sentMessages.indexOf(msg), 1);
printStatus('green');
// if(!stillSending && sentMessages.length == 0){
// websocket.close(1000);
// }
} else {
stopSending();
websocket.close();
changeBG('red');
console.log("not found msg: " + msg);
console.log(sentMessages);
}
}
function stopSending() {
stillSending = false;
}
function onError(evt) {
changeBG('red');
writeToScreen(evt)
console.log("Error :: " + evt);
}
function doSend() {
totalSent += 1;
var rndMsg = getRandMsg();
sentMessages.push(rndMsg);
websocket.send(rndMsg);
printStatus('green');
if(totalSent == MSG_TARGET){
stopSending()
}
if(stillSending) doSend();
}
function printStatus(color){
changeBG(color);
var stck_len = sentMessages.length;
var received = totalSent - stck_len;
writeToScreen("SENT: " + totalSent + "<br>WAIT: " + stck_len + "<br>RCVD: " + received);
}
function writeToScreen(message) {
document.body.innerHTML = message;
}
function changeBG(color) {
document.body.style.background = color;
}
function getRandMsg() {
return Math.random().toString(36).substring(7);
}
var params = window.location.hash.replace('#','').split('|');
MSG_TARGET = parseInt(params[0])|| 1000;
setTimeout("doSend()", 1000);
</script>
</html>