-
Notifications
You must be signed in to change notification settings - Fork 9
/
basic.html
38 lines (33 loc) · 998 Bytes
/
basic.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
<div id="output"></div>
<script>
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
//Create and open the socket
ws = new WebSocket("ws://localhost:6437/");
// On successful connection
ws.onopen = function(event) {
document.getElementById("main").style.visibility = "visible";
document.getElementById("connection").innerHTML = "WebSocket connection open!";
};
// On message received
ws.onmessage = function(event) {
var obj = JSON.parse(event.data);
var str = JSON.stringify(obj, undefined, 2);
document.getElementById("output").innerHTML = '<pre>' + str + '</pre>';
};
// On socket close
ws.onclose = function(event) {
ws = null;
document.getElementById("main").style.visibility = "hidden";
document.getElementById("connection").innerHTML = "WebSocket connection closed";
}
</script>
<style>
body {
font-size: 8px;
font-family: sans-serif;
}
</style>