forked from tpunt/pht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwstest.html
55 lines (43 loc) · 1.34 KB
/
wstest.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
<!DOCTYPE html>
<!-- http://www.websocket.org/echo.html -->
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:9000";
var clients = [];
var client_count = 10;
var out = null;
function init() {
out = document.getElementById("output");
for (var i = 0; i < client_count; i++) {
testWebSocket(i);
}
}
function testWebSocket(i) {
clients[i] = new WebSocket(wsUri);
clients[i].onopen = function(evt) { onOpen(evt, i) };
clients[i].onclose = function(evt) { onClose(evt) };
clients[i].onmessage = function(evt) { onMessage(evt, i) };
clients[i].onerror = function(evt) { onError(evt) };
}
function onOpen(evt, i) {
doSend(i, "WebSocket rocks");
}
function onClose(evt) {
}
function onMessage(evt, i) {
doSend(i, "WebSocket rocks");
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(i, message) {
var channel = (Math.floor((Math.random() * 5) + 1) - 1);
message += " on channel " + channel
clients[i].send(JSON.stringify({data: message, channel: channel}));
}
window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>