-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient-JustLog.html
executable file
·82 lines (65 loc) · 1.62 KB
/
client-JustLog.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
<html>
<head>
<title>Uber Simple Websockets</title>
<!-- Websocket -->
<script type="text/javascript">
// Config
var port = 9000;
var host = "ws://127.0.0.1:"+port; // No need to change this if using localhost
var consoleLogOnScreen = true; // If true shows the console.log in an HTML element
//Declare Variables
var socket;
var explodedValues = [0,0,0,0]; //initial value for the plot = 0
function init() {
if (consoleLogOnScreen) {
// Function to output console.log to html element (logs on screen)
// From: http://stackoverflow.com/a/20256785/1709835
(function () {
if (!console) {
console = {};
}
var old = console.log;
var logger = document.getElementById('log');
console.log = function (message) {
logger.innerHTML += message + '<br />';
}
})();
}
// Websocket!
try {
socket = new WebSocket(host);
console.log('WebSocket status '+socket.readyState);
socket.onopen = function(msg) {
console.log("Welcome - status "+this.readyState);
};
socket.onmessage = function(msg) {
console.log("Message Received: "+msg.data);
};
socket.onclose = function(msg) {
console.log("Disconnected - status "+this.readyState);
};
}
catch(ex){
console.log(ex);
}
}
function quit(){
if (socket != null) {
console.log("Close Socket");
socket.close();
socket=null;
}
}
function reconnect() {
quit();
init();
}
</script>
</head>
<body onload="init()">
<h3>Uber Simple Websockets - Display Logs</h3>
<button onclick="reconnect()">Reconnect</button>
<h4>Log</h4>
<div id="log"></div>
</body>
</html>