Files
2026-05-18 13:06:57 -04:00

150 lines
3.6 KiB
JavaScript

/* Skald client example - audio-only. */
/**
* The main function.
*
* @param {string} url
*/
async function start(url) {
// fetch the hall information
let r = await fetch(url + ".status");
if(!r.ok) {
throw new Error(`${r.status} ${r.statusText}`);
}
let status = await r.json();
// parse a token in the URL.
let token = null;
let parms = new URLSearchParams(window.location.search);
if(parms.has('token'))
token = parms.get('token');
// connect to the server
if(token) {
serverConnect(status, token);
} else if(status.authPortal) {
window.location.href = status.authPortal
return;
} else {
serverConnect(status, null);
}
}
/**
* Display the connection status.
*
* @param {string} status
*/
function displayStatus(status) {
let c = document.getElementById('status');
c.textContent = status;
}
/**
* Connect to the server.
*
* @param {Object} status
* @param {string} token
*/
function serverConnect(status, token) {
let conn = new ServerConnection();
conn.onconnected = async function() {
displayStatus('Connected');
let creds = token ?
{type: 'token', token: token} :
{type: 'password', password: ''};
await this.join("public", "example-user", creds);
};
conn.onchat = onChat;
conn.onusermessage = onUserMessage;
conn.onclose = function() {
displayStatus('Disconnected');
}
conn.onjoined = onJoined;
conn.connect(status.endpoint);
}
/**
* Called whenever we receive a chat message.
*/
function onChat(id, dest, username, time, privileged, history, kind, message) {
let p = document.createElement('p');
p.textContent = `${username}${dest ? ' → ' + dest : ''}: ${message}`;
let container = document.getElementById('chat');
container.appendChild(p);
}
/**
* Called whenever we receive a user message.
*/
function onUserMessage(id, dest, username, time, privileged, kind, error, message) {
switch(kind) {
case 'kicked':
case 'error':
case 'warning':
case 'info':
if(!privileged) {
console.error(`Got unprivileged message of kind ${kind}`);
return;
}
displayError(message);
break;
case 'clearchat':
if(!privileged) {
console.error(`Got unprivileged message of kind ${kind}`);
return;
}
document.getElementById('chat').textContent = '';
break;
}
}
/**
* Called when we join or leave a hall.
*/
async function onJoined(kind, hall, perms, status, data, error, message) {
switch(kind) {
case 'fail':
displayError(message);
this.close();
break;
case 'redirect':
this.close();
document.location.href = message;
return;
case 'leave':
displayStatus('Connected');
this.close();
break;
case 'join':
case 'change':
displayStatus(`Connected as ${this.username} in hall ${this.hall}.`);
// request audio from the server
this.request({'': ['audio']});
break;
default:
displayError(`Unexpected state ${kind}.`);
this.close();
break;
}
}
/**
* Display an error message.
*
* @param {string} message
*/
function displayError(message) {
document.getElementById('error').textContent = message;
}
document.getElementById('start').onclick = async function(e) {
let button = /** @type{HTMLButtonElement} */(this);
button.hidden = true;
try {
await start("/hall/public/");
} catch(e) {
displayError(e);
};
}