A complete JavaScript example
This program is short and does the whole round: it connects, tries to get in, creates the account only if it does not exist, reads the welcome, makes a small movement suited to the movement mode of the game and prints what happens. It works in a browser and in Node 22 or later, because it uses the standard WebSocket. Change the address with the one you find in the panel, in Overview.
// A minimal client for Nyra.
const ADDRESS = "ws://127.0.0.1:3000"; // copy the address from the panel
const NAME = "mario-88";
const EMAIL = "mario@esempio.it";
const PASSWORD = "una-password";
const ws = new WebSocket(ADDRESS);
const others = new Map(); // the other players, by playerId
let me = null; // my player
let mode = null; // tile, platform or eightdir
let triedRegister = false; // have we already tried to create the account?
function send(msg) {
ws.send(JSON.stringify(msg));
}
function login() {
send({ type: "login", identifier: NAME, password: PASSWORD });
}
function moveABit() {
if (mode === "tile") {
send({ type: "move-input", dx: 1, dy: 0 });
} else if (mode === "platform") {
send({ type: "input-state", left: false, right: true, jump: false, run: false });
setTimeout(() => send({ type: "input-state", left: false, right: false, jump: false, run: false }), 1000);
} else if (mode === "eightdir") {
send({ type: "input-angle", moving: true, angle: 45, run: false });
setTimeout(() => send({ type: "input-angle", moving: false }), 1000);
}
}
ws.addEventListener("open", () => {
// First we try to get in. The account is created only if it does not exist yet.
login();
});
ws.addEventListener("message", (event) => {
const msg = JSON.parse(event.data);
switch (msg.type) {
case "register-success":
login();
break;
case "auth-error":
if (msg.context === "login" && msg.reason === "invalid-credentials" && !triedRegister) {
// The name or the password are not right: maybe the account does not exist. Let us try to create it.
triedRegister = true;
send({ type: "register", username: NAME, email: EMAIL, password: PASSWORD });
} else {
console.log("Sign-in refused:", msg.context, msg.reason);
ws.close();
}
break;
case "welcome":
if (msg.protocolVersion !== 1) {
console.log("Protocol version not supported:", msg.protocolVersion);
ws.close();
return;
}
mode = msg.movementMode;
me = { id: msg.playerId, x: msg.x, y: msg.y };
for (const p of msg.otherPlayers) {
others.set(p.playerId, p);
}
console.log("Inside as", msg.username, "in mode", mode, "with", others.size, "other players");
moveABit();
break;
case "player-joined":
others.set(msg.playerId, msg);
console.log("Joined", msg.username);
break;
case "player-moved": {
// In tile the arrival position is toX, toY. In the other modes it is x, y.
const x = msg.toX !== undefined ? msg.toX : msg.x;
const y = msg.toY !== undefined ? msg.toY : msg.y;
if (msg.playerId === me.id) {
me.x = x;
me.y = y;
console.log("I am at", x, y);
} else if (others.has(msg.playerId)) {
const p = others.get(msg.playerId);
p.x = x;
p.y = y;
console.log("Another player is at", x, y);
}
break;
}
case "move-rejected":
// In tile: the previous step is not over, or the cell is outside the map.
console.log("Step refused:", msg.reason);
break;
case "player-disconnected":
others.delete(msg.playerId);
console.log("A player left");
break;
case "disconnect":
console.log("The server disconnects you:", msg.reason);
break;
default:
// A type this client does not know: we write it, we do not throw it away.
console.log("Message not handled:", msg.type);
}
});
ws.addEventListener("close", (event) => {
console.log("Connection closed, code", event.code);
});In tile, if a step is refused, the server sends move-rejected with the reason: the example writes it in the log.
Note
The program tries the login first and registers only if it has to, because the server accepts at most 3 registrations an hour from the same address and counts the ones that fail too.
Note
If your game uses the tile mode, the character takes a single step. To move again, send another move-input when the step is over.
Note
In platform and in 8 directions the line «I am at» appears many times, about every 50 milliseconds while the character moves, and with decimal numbers. In 8 directions the last position repeats twice: it is the stop message. In tile the numbers are multiples of the cell size.
What the example prints with a second player: «Joined …», then «Another player is at …» at every move of theirs, then «A player left». With a double login: «The server disconnects you: duplicate-login» and «Connection closed, code 4001». With an orderly stop of the server: «The server disconnects you: server-stopping» and «Connection closed, code 4003». With an abrupt stop: only «Connection closed, code 1006».