This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
/
client.html
142 lines (123 loc) · 3.37 KB
/
client.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
135
136
137
138
139
140
141
142
<!doctype html>
<html>
<head>
<title>node-websocket-server test</title>
<style type="text/css">
#rockbar {
position: fixed;
top: 0;
left: 20px;
background: #fff;
padding: 0.5em;
display: block;
}
#warning {
padding: 1em;
border: 2px solid red;
}
</style>
</head>
<body>
<p id="warning">
<strong>WARNING:</strong> run the Spam Test at your own risk, see below for more information.<br /><br />
Spam Test: The spam test rapidly spends 1 million messages to the websocket-server at a near instant interval, as such, any browsers connected to the server at the time of a spam test may crash or freeze up. The websocket server should not crash, but this is not 100% guaranteed.<br/><br />
The author does not take responsibility for any damage or data loss caused.</p>
<div id="rockbar">
<a href="#" id="send">Send Something</a> | <a href="#" id="spam">Run Spam Test</a> | <a href="#" id="close">Close Connection.</a> | <a href="#" id="open">Open Connection.</a>
</div>
<div id="log"></div>
<script type="text/javascript">
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
function timestamp() {
var d = new Date();
return [
d.getDate(),
months[d.getMonth()],
[ pad(d.getHours())
, pad(d.getMinutes())
, pad(d.getSeconds())
, (d.getTime() + "").substr( - 4, 4)
].join(':')
].join(' ');
};
function scrollToBottom() {
window.scrollBy(0, document.body.scrollHeight - document.body.scrollTop);
};
function log(data){
output_log.innerHTML += timestamp()+": "+data+"<br />";
scrollToBottom();
}
var conn, recvd, connections = 0;
var output_log = document.getElementById("log");
var connect = function() {
if (window["WebSocket"]) {
recvd = 0;
//host = (document.location.host != "" ? document.location.host : "localhost:8000");
host = "localhost:8000";
conn = new WebSocket("ws://"+host+"/test");
conn.onmessage = function(evt) {
log(evt.data);
// conn.close();
// log(connections++);
// connect();
// if(recvd == 0){
// conn.id = parseInt(evt.data.match(/\: ([0-9]+)/)[1], 10);
// }
// recvd++;
};
conn.onerror = function() {
log("error", arguments);
};
conn.onclose = function() {
log("closed");
};
conn.onopen = function() {
log("opened");
};
}
};
document.getElementById("send").addEventListener("click", function(e) {
if (conn) {
setTimeout(function() {
conn.send("test message");
log("<"+conn.id+"> "+"test message");
}, 0);
}
e.preventDefault();
return false;
}, false);
document.getElementById("spam").addEventListener("click", function() {
if (conn) {
for (var i = 0; i < 100000; ++i) {
(function(n) {
setTimeout(function() {
conn.send("t" + n);
//log("<"+conn.id+"> "+"t "+n);
}, 0);
})(i);
}
}
return false;
}, true);
document.getElementById("close").addEventListener("click", function(e) {
if (conn) {
conn.close();
conn = false;
}
e.preventDefault();
return false;
}, false);
document.getElementById("open").addEventListener("click", function(e) {
if (!conn) {
connect();
}
e.preventDefault();
return false;
}, false);
window.onload = connect;
</script>
</body>
</html>