Skip to content

Commit

Permalink
Tell people how many people are currently in the room. Fixes #165
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkaufman committed Jan 8, 2021
1 parent 31efee5 commit 3f92a5e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
29 changes: 28 additions & 1 deletion html/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ function joinBucket(i) {
}
}

function server_api_path() {
return new URL(serverPath.value, document.location).href;
}

function updateCurrentUsers() {
var xhr = new XMLHttpRequest();
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"));
let roomText = "The room is currently empty.";
if (x_audio_metadata.n_connected_users) {
if (x_audio_metadata.n_connected_users == 1) {
roomText = "There is currently 1 person in the room.";
} else {
roomText = "There are currently " + x_audio_metadata.n_connected_users +
" people in the room.";
}
}
window.currentUsers.innerText = roomText;
}
};
xhr.send();
}
updateCurrentUsers();

const user_bucket_index = {}; // userid -> bucket index (-1 means unbucketed)
const bucket_divs = []; // bucket index -> bucket div

Expand Down Expand Up @@ -1337,7 +1364,7 @@ function connect_twilio() {
}

async function start_singing() {
var final_url = new URL(serverPath.value, document.location).href;
var final_url = server_api_path();

singer_client = new bb.SingerClient({
context: bucket_ctx,
Expand Down
2 changes: 1 addition & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ <h3>Settings that affect everyone</h3>
<div id=tutorial>
<div id=tutorial_questions>
Welcome! This is a program for singing with people over the
Internet. <span id=eventWelcome></span>
Internet. <span id=eventWelcome></span> <span id=currentUsers></span>

<p>
<br>
Expand Down
13 changes: 13 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@

SUPPORT_SERVER_CONTROL = False

# The maximum number of users to allow to join. This is enforced on a
# best-effort basis by the client. If many people are calibrating at
# the same time this will be exceeded, because we only check before
# calibration.
MAX_USERS = 22 # XXX needs tuning

try:
# Grab these on startup, when they are very very likely to be the actual
# running version.
Expand Down Expand Up @@ -881,6 +887,13 @@ def handle_post(in_data, query_string, print_status, client_address=None) -> Tup
raw_params = urllib.parse.parse_qs(query_string, strict_parsing=True)
query_params = clean_query_params(raw_params)

action = query_params.get("action", None)
if action == "status":
return np.zeros(0, np.uint8), json.dumps({
"n_connected_users": len(active_users()),
"max_users": MAX_USERS,
})

userid = query_params.get("userid", None)
if userid is not None:
if int(userid) < 0 or int(userid) > 0xffff_ffff_ffff_ffff:
Expand Down

0 comments on commit 3f92a5e

Please sign in to comment.