Skip to content

Mostra un messaggio fuori dalla webapp ​

Una persona che segue un vecchio link o un motore di ricerca possono aprire l'indirizzo della tua vista in una scheda del browser senza passare dalla webapp. Fuori dalla webapp non c'è nessun mondo e connectToHost() viene rifiutata con il codice 'no-host'. Invece di lasciare la pagina vuota, di' che cos'è la pagina e metti un link al mondo in UNaIVERSE.

L'esempio non ha un mondo finto e fa la chiamata vera. Questa pagina della documentazione non è la webapp e la chiamata aspetta una risposta per 5 secondi prima che l'esempio mostri il suo messaggio.

jsx
import { useWorld } from './useWorld.js';

// The address of your world in UNaIVERSE, for people who open your view on its own.
const WORLD_LINK = 'https://unaiverse.io';

export default function App() {
	// This recipe is about the real call, as in the webapp, and has no fake world.
	const { world, error } = useWorld();

	// A page opened alone, outside the webapp, has no world, and the code is 'no-host'.
	if (error?.code === 'no-host') {
		return (
			<>
				<h2>This page is the view of Arena</h2>
				<p>It works inside UNaIVERSE, where Arena lives. Open Arena there to take part.</p>
				<a href={WORLD_LINK} target="_blank" rel="noopener">Open Arena in UNaIVERSE</a>
			</>
		);
	}
	// The other code, 'unsupported', comes from a webapp that does not speak this version of the kit.
	if (error) return <p>This view cannot start here ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return <h2>{world.name}</h2>;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The address of your world in UNaIVERSE, for people who open your view on its own.
const WORLD_LINK = 'https://unaiverse.io';

const status = document.createElement('p');
status.textContent = 'Connecting…';
document.body.append(status);

try {
	// This recipe is about the real call, as in the webapp, and has no fake world.
	const world = await connectToHost();
	const title = document.createElement('h2');
	status.replaceWith(title);
	world.subscribe((node) => {
		title.textContent = node.name;
	});
} catch (error) {
	if (error.code === 'no-host') {
		// A page opened alone, outside the webapp, has no world, and the code is 'no-host'.
		const title = document.createElement('h2');
		title.textContent = 'This page is the view of Arena';
		const text = document.createElement('p');
		text.textContent = 'It works inside UNaIVERSE, where Arena lives. Open Arena there to take part.';
		const link = document.createElement('a');
		link.href = WORLD_LINK;
		link.target = '_blank';
		link.rel = 'noopener';
		link.textContent = 'Open Arena in UNaIVERSE';
		status.replaceWith(title, text, link);
	} else {
		// The other code, 'unsupported', comes from a webapp that does not speak this version of the kit.
		status.textContent = `This view cannot start here (${error.code}).`;
	}
}
Live, on a fake world
Open

Come funziona ​

connectToHost() viene rifiutata con un errore il cui code dice il motivo. Il codice è 'no-host' quando la pagina non è dentro la webapp o quando la webapp non ha risposto entro timeoutMs (5 secondi di default). Una pagina aperta da sola in una scheda riceve 'no-host' subito perché non ha una finestra genitore da aspettare. L'altro codice, 'unsupported', arriva da una webapp che non accetta la versione del tuo kit. Questo codice riguarda chi sviluppa più che la persona e richiede solo un messaggio breve.

Il link si apre in una nuova scheda (target="_blank") e lascia la pagina com'è. Metti l'indirizzo del tuo mondo in WORLD_LINK.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.