Switched to recommended server endpoint.

This commit is contained in:
flash 2025-04-22 22:12:53 +00:00
parent da33816ee4
commit af9d917157
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
3 changed files with 70 additions and 10 deletions

View file

@ -1,6 +1,6 @@
#include awaitable.js
const MamiConnectionManager = function(client, settings, urls, eventTarget) {
const MamiConnectionManager = function(client, settings, flashii, eventTarget) {
const validateClient = value => {
if(typeof value !== 'object' || value === null)
throw 'client must be a non-null object';
@ -10,8 +10,6 @@ const MamiConnectionManager = function(client, settings, urls, eventTarget) {
if(typeof settings !== 'object' || settings === null)
throw 'settings must be a non-null object';
if(!Array.isArray(urls))
throw 'urls must be an array';
if(typeof eventTarget !== 'object' || eventTarget === null)
throw 'eventTarget must be a non-null object';
@ -41,14 +39,8 @@ const MamiConnectionManager = function(client, settings, urls, eventTarget) {
url = undefined;
};
$arrayShuffle(urls);
const attempt = () => {
started = Date.now();
url = urls[attempts % urls.length];
if(url.startsWith('//'))
url = location.protocol.replace('http', 'ws') + url;
const attemptNo = attempts + 1;
timeout = setTimeout(() => {
@ -65,6 +57,16 @@ const MamiConnectionManager = function(client, settings, urls, eventTarget) {
});
try {
({ uri: url } = await flashii.v1.chat.servers.recommended({
proto: 'sockchat',
fresh: attempts > 3,
}));
} catch(ex) {}
try {
if(typeof url !== 'string')
throw {};
const result = await client.connect(url);
if(typeof result === 'boolean' && !result)
throw {};

View file

@ -68,6 +68,14 @@ const Flashii = function(baseUrl) {
fii.v1 = {};
const verifyChatServerId = id => {
if(typeof id === 'number')
id = id.toString();
if(/^([^0-9]+)$/gu.test(id))
throw new Error('id argument is not an acceptable chat server id.');
return id;
};
fii.v1.chat = {};
fii.v1.chat.login = function({ redirect=null, assign=true }) {
return new Promise(resolve => {
@ -85,6 +93,56 @@ const Flashii = function(baseUrl) {
resolve(url);
});
};
fii.v1.chat.servers = async function({ proto=null, secure=null, fields=null, fresh=false }) {
const params = {};
if(proto !== null && typeof proto !== 'string')
throw new Error('proto must be a string or null');
params['proto'] = proto;
if(secure !== null)
params['secure'] = secure ? '1' : '0';
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
if(status > 299)
throw new Error(`Failed to fetch chat server list with error code ${status}.`);
return body;
};
fii.v1.chat.servers.server = async function({ id, fields=null, fresh=false }) {
id = verifyChatServerId(id);
const { status, body } = await send({ method: 'GET', path: `/v1/chat/servers/${id}`, fields, fresh });
if(status === 400)
throw new Error('Fields argument contains unsupported value.');
if(status === 404)
throw new Error('No chat server with that id could be found.');
if(status > 299)
throw new Error(`Failed to fetch server list with error code ${status}.`);
return body;
};
fii.v1.chat.servers.recommended = async function({ proto, secure=null, fields=null, fresh=false }) {
if(typeof proto !== 'string')
throw new Error('proto must be a string');
const params = { proto };
if(secure !== null)
params['secure'] = secure ? '1' : '0';
const { status, body } = await send({ method: 'GET', path: '/v1/chat/servers/recommended', fields, fresh, params });
if(status === 400)
throw new Error('An argument contains an unsupported value.');
if(status > 299)
throw new Error(`Failed to fetch chat server list with error code ${status}.`);
return body;
};
fii.v1.chat.token = async function() {
const { status, body } = await send({ method: 'GET', path: '/v1/chat/token', authed: true, fresh: true });

View file

@ -849,7 +849,7 @@ const MamiInit = async args => {
};
const sockChat = new MamiSockChat(ctx.events.scopeTo('sockchat'));
const conMan = new MamiConnectionManager(sockChat, settings, futami.get('servers'), ctx.events.scopeTo('conn'));
const conMan = new MamiConnectionManager(sockChat, settings, flashii, ctx.events.scopeTo('conn'));
ctx.conMan = conMan;
ctx.events.watch('form:send', ev => {