Skip to content

Mostra chi è online ​

Un mondo elenca i suoi membri in world.members, ognuno con un nome, un ruolo e uno status che dice se il membro è online in questo momento. L'esempio conta i membri online e mostra ogni membro attenuando quelli che non sono online. In un mondo finto ogni membro è sempre online. Nella webapp invece la lista cambia man mano che i membri entrano ed escono.

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

// The fake world is a club with three members. In a fake world, members are always online.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Ada', role: 'player', isMaster: true },
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
	],
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: club } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// Drawn again at every change, including when someone comes or goes.
	const members = world.members ?? [];
	// A member's status is 'online', 'connecting' or 'offline'.
	const online = members.filter((member) => member.status === 'online');
	return (
		<>
			<h3>Online: {online.length} of {members.length}</h3>
			<ul style={{ listStyle: 'none', padding: 0 }}>
				{members.map((member) => {
					const isOnline = member.status === 'online';
					return (
						<li key={member.id} style={{ opacity: isOnline ? 1 : 0.5 }}>
							{isOnline ? '●' : '○'} {member.name ?? 'Someone'}, {member.role ?? 'member'}
							{member.isMaster ? ' (master)' : ''}: {member.status}
						</li>
					);
				})}
			</ul>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The fake world is a club with three members. In a fake world, members are always online.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Ada', role: 'player', isMaster: true },
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
	],
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: club } });

const title = document.createElement('h3');
const list = document.createElement('ul');
list.style.listStyle = 'none';
list.style.padding = '0';
document.body.append(title, list);

// Called at once, then after every change, including when someone comes or goes.
world.subscribe((node) => {
	const members = node.members ?? [];
	// A member's status is 'online', 'connecting' or 'offline'.
	const online = members.filter((member) => member.status === 'online');
	title.textContent = `Online: ${online.length} of ${members.length}`;
	list.replaceChildren(...members.map((member) => {
		const isOnline = member.status === 'online';
		const item = document.createElement('li');
		item.style.opacity = isOnline ? '1' : '0.5';
		item.textContent = `${isOnline ? '●' : '○'} ${member.name ?? 'Someone'}, ${member.role ?? 'member'}`
			+ `${member.isMaster ? ' (master)' : ''}: ${member.status}`;
		return item;
	}));
});
Live, on a fake world
Open

Come funziona ​

  • member.status vale 'online', 'connecting' o 'offline'. subscribe chiama di nuovo il suo listener quando uno status cambia e quando un membro entra o esce.
  • member.role è il ruolo del membro nel mondo e member.isMaster è true per i master del mondo. I ruoli del mondo sono in world.roles e il ruolo della persona è in world.myRole.
  • Finché non arriva il profilo di un membro il suo name può essere null per un momento. Al suo posto l'esempio mostra 'Someone'.
  • La vista di un agente non riceve nessuna lista (solo un mondo ha members) e l'esempio legge node.members ?? [] per coprire questo caso.

Vedi anche ​

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