Skip to content

Commit

Permalink
protocol: move metadata to response body. Fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkaufman committed Jan 25, 2021
1 parent 77863db commit 382e09c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 2 additions & 3 deletions html/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ function updateCurrentUsers() {
xhr.open('POST', server_api_path() + "?action=status", true);
xhr.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
const x_audio_metadata = JSON.parse(this.getResponseHeader(
"X-Audio-Metadata"));
const x_audio_metadata = JSON.parse(this.response);

if (x_audio_metadata.n_connected_users >= x_audio_metadata.max_users) {
switch_app_state(APP_ROOM_FULL);
Expand Down Expand Up @@ -348,7 +347,7 @@ window.lyricsEntryCancel.addEventListener("click", () => {
});

window.lyricsEntryOk.addEventListener("click", () => {
if (window.lyricsEntryBox.value.length > 2500) {
if (window.lyricsEntryBox.value.length > 8500) {
window.lyricsTooLong.style.display = "block";
} else {
window.lyricsEntry.style.display = "none";
Expand Down
16 changes: 11 additions & 5 deletions html/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,22 @@ function handle_xhr_result(xhr, resolve, reject) {
--xhrs_inflight;

if (xhr.status == 200) {
var metadata = JSON.parse(xhr.getResponseHeader("X-Audio-Metadata"));
let metadata = JSON.parse(xhr.getResponseHeader("X-Audio-Metadata"));
let data = xhr.response;

const metadata_len = metadata.metadata_len;
if (metadata_len) {
metadata = JSON.parse(new TextDecoder('utf8').decode(
data.slice(0, metadata_len)));
data = data.slice(metadata_len);
}

if (LOG_ULTRA_VERBOSE) {
console.debug("SPAM", "metadata:", metadata);
console.debug("SPAM", "Got XHR response w/ ID:", xhr.debug_id, "result:", xhr.response, " -- still in flight:", xhrs_inflight);
}

return resolve({
metadata: metadata,
data: xhr.response
});
return resolve({metadata, data});
} else {
console.error("XHR failed w/ ID:", xhr.debug_id, "stopping:", xhr, " -- still in flight:", xhrs_inflight);
var metadata_raw = xhr.getResponseHeader("X-Audio-Metadata");
Expand Down
10 changes: 8 additions & 2 deletions server_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,20 @@ def do_POST(environ, start_response) -> None:
print("Request raised exception!\nParams:", query_string, "\n", traceback.format_exc(), file=sys.stderr)
return die500(start_response, e)

combined_data = x_audio_metadata.encode('utf-8') + data

simple_x_audio_metadata = json.dumps({
"metadata_len": len(x_audio_metadata)
})

start_response(
'200 OK',
[("Access-Control-Allow-Origin", "*"),
("Access-Control-Max-Age", "86400"),
("Access-Control-Expose-Headers", "X-Audio-Metadata"),
("X-Audio-Metadata", x_audio_metadata),
("X-Audio-Metadata", simple_x_audio_metadata),
("Content-Type", "application/octet-stream")])
return data,
return combined_data,

def application(environ, start_response):
global backend
Expand Down

0 comments on commit 382e09c

Please sign in to comment.