-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
65 lines (53 loc) · 1.37 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>WebSocket Test</title>
<link rel="stylesheet" href="">
</head>
<body>
<h2>WebSocket Test</h2>
<div id="output"></div>
<script type="text/javascript">
var wsUri = "ws://echo.websocket.org/";
var output;
function init () {
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket () {
websocket = new WebSocket(wsUri);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onerror = onError;
websocket.onmessage = onMessage;
}
function onOpen (event) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks!");
}
function onClose (event) {
writeToScreen("DISCONNECTED");
}
function onMessage (event) {
writeToScreen('<span style="color: blue;">RESPONSE: ' + event.data + '</span>');
websocket.close();
}
function onError (event) {
writeToScreen('<span style="color: blue;">ERROR: ' + event.data + '</span>');
}
function doSend (message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen (message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
</body>
</html>