-
Notifications
You must be signed in to change notification settings - Fork 6
/
test-7.9+.html
143 lines (130 loc) · 4.16 KB
/
test-7.9+.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test 7.9 API</title>
</head>
<body>
<script>
let host = 'localhost';
let port = 60157;
let apiRoot = `http://${host}:${port}`;
let useApi = false;
// function* creates an iterator that can use the `yield` keyword
async function* chunkIterator(response) {
const reader = response.body.getReader();
const utf8Decoder = new TextDecoder('utf-8');
let accumulator = '';
// propresenter chunk terminator is double enter
const re = /\r\n\r\n/gm;
let match;
for (;;) {
let { value: chunk, done: readerDone } = await reader.read();
chunkString = chunk ? utf8Decoder.decode(chunk) : '';
console.log(`
----- received ${chunk.length} bytes ----------------
${chunkString}
-----------------------------------------------------`);
accumulator += chunkString;
let accumulated = accumulator.split('\r\n\r\n');
if (accumulated.length < 2) {
if (readerDone) break;
continue;
}
// retain the final item for later
console.log(accumulated);
accumulator = accumulated.splice(accumulated.length - 1)[0];
// yield the rest
for (let singleChunk of accumulated) {
yield singleChunk;
}
}
}
async function call(endpoint, config = null, onchunk = null) {
let url = `${apiRoot}${endpoint}`;
if (onchunk != null && config?.method != 'POST') url += '?chunked=true';
console.log(url);
let response = await fetch(url, config);
if (response.ok) {
if (onchunk == null) {
response.data = await response.json();
console.log(response.data);
} else {
for await (let chunk of chunkIterator(response)) {
onchunk(chunk);
}
}
}
return response;
}
async function getVersion() {
let response = await call(`/version`);
if (response.ok) {
useApi = true;
subscribe();
}
}
function connect() {
getVersion();
}
async function subscribe() {
// capture/status
// look/current
// messages
// playlist/current
// presentation/current
// presentation/slide_index
// stage/message
// status/layers
// status/stage_screens
// status/audience_screens
// status/screens
// status/slide
// timers
// timers/current
// timer/system_time
// timer/video_countdown
call(
'/v1/status/updates',
{
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
Origin: 'localhost',
},
body: JSON.stringify([
// 'capture/status',
// 'look/current',
// // the chunked messages endpoint doesn't work in 7.9 beta
// // 'messages',
// 'playlist/current',
// this will yield too much data, the response chunks get merged together wrong
// see: https://gist.github.com/jeffmikels/9f01e9f938dbe9af9072ca9be9cec7e0
'presentation/current',
'presentation/slide_index',
// 'stage/message',
// 'status/layers',
// 'status/stage_screens',
// 'status/audience_screens',
// 'status/screens',
// 'status/slide',
// 'timers',
// 'timers/current',
// 'timer/system_time',
// 'timer/video_countdown',
]),
},
handleChunk,
);
}
function handleChunk(chunk) {
console.log(`HANDLING CHUNK: ${chunk}`);
let chunkData = JSON.parse(chunk);
console.log(chunkData);
}
</script>
</body>
</html>