connectLoopbackfunctionexperimentalnacatamalon/net
connectLoopback(server: TServer, options: TLoopbackOptions) => TNetConnectionConnects a player's game to a server in the same page or process, with no network at all: a line
in memory, given to server.accept at once, whose other end goes to useNet().connect. For trying
an online game in one page (the server's room and two players side by side), and for tests.
Messages still go as text, so what JSON cannot carry fails here as it would over the internet, and
still arrive later, in order, never in the middle of a frame. delay makes the line as slow as a
real one, to see how the game feels from far away.
Parameters
serverTServeroptionsTLoopbackOptions
Example
import { createScene, type TSceneFn } from 'nacatamalon';
import { connectLoopback, createServer, useNet } from 'nacatamalon/net';
// The same scene runs in the server's room and in the player's page.
const Lobby: TSceneFn = () => {
const net = useNet();
if (net.role === 'client') {
// Into the server below, 50 ms away each way.
net.connect(connectLoopback(server, { delay: 0.05 }), { room: 'lobby' });
}
return createScene();
};
const server = createServer({ width: 320, height: 180, scenes: { Lobby }, step: 20 });