-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
107 lines (107 loc) · 2.13 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
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
<!doctype html>
<html>
<head>
<style>
body {
margin: 0;
font-family: monospace;
font-size: 14px;
color: #dcdcdc;
background-color: #240029;
}
#out {
padding: 3px;
}
#controls {
background-color: black;
border-bottom: 1px gray solid;
height: 20px;
padding: 3px 3px 0 3px;
}
#connectionState {
width: 128px;
}
.control {
float: left;
}
.clickable {
text-decoration: underline;
cursor: pointer;
}
.line {
white-space: pre-wrap;
}
</style>
<script src="jquery.min.js"></script>
<script>
ES = null;
LAST_EVENT_ID = null;
function connect() {
ES = new EventSource('/events');
$(ES).on('line', function(event) {
event = event.originalEvent;
var lastEventId = Number(event.lastEventId)
if (lastEventId < LAST_EVENT_ID)
return;
LAST_EVENT_ID = lastEventId
console.log(event);
var divEl = $('<div class="line">');
divEl.text(event.data);
var stdoutEl = $('#out');
stdoutEl.append(divEl);
});
$(ES).on('eof', function(event) {
event = event.originalEvent;
console.log(event);
ES.close();
ES = null;
updateStatus();
$('#connectionState').text('EOF');
});
$(ES).on('open', function() {
updateStatus();
});
$(ES).on('error', function(e) {
updateStatus();
});
}
function updateStatus() {
if (ES == null) {
$('#connect').show();
$('#disconnect').hide();
} else if (ES.readyState == ES.CLOSED) {
$('#connectionState').text('Disconnected');
$('#connect').show();
$('#disconnect').hide();
} else if (ES.readyState == ES.OPEN) {
$('#connectionState').text('Listening');
$('#connect').hide();
$('#disconnect').show();
} else { // CONNECTING
$('#connectionState').text('Connecting');
$('#connect').hide();
$('#disconnect').show();
}
}
$(function() {
connect();
$('#disconnect').on('click', function() {
ES.close();
updateStatus();
});
$('#connect').on('click', function() {
connect();
updateStatus();
});
});
</script>
</head>
<body>
<div id="controls">
<div id="connectionState" class="control">Disconnected</div>
<div id="disconnect" class="control clickable">Disconnect</div>
<div id="connect" class="control clickable" style="display: none">Connect</div>
</div>
<div id="out"></div>
</body>
</html>