-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
42 lines (35 loc) · 962 Bytes
/
stream.js
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
function readStream()
{
const evtSource = new EventSource('/Counter/Stream');
evtSource.onmessage = function(event) {
const el = document.getElementById('counter');
const { value } = JSON.parse(event.data);
el.textContent = value;
}
}
function startCounter()
{
fetch('/Counter/Start', { method: 'PUT' });
}
function stopCounter()
{
fetch('/Counter/Stop', { method: 'PUT' });
}
function setCounter()
{
async function putData(url = '', data = {}) {
const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
});
// parses JSON response into native JavaScript objects
return response.json();
}
const el = document.getElementById('set-value');
const value = Number(el.value);
putData('/Counter', { value });
}