How the connection works
The address
The client connects to the server with a WebSocket connection. You find the address in the panel, in Overview, in the Address to connect the game box: copy it with the Copy button.
- On your computer the address looks like
ws://127.0.0.1:3000(the port is usually 3000). It works only on that computer. - Online it is the public address of your game. It usually starts with
wss://, because the protected connection is handled by a service in front of the server. The game server itself speaksws://.
Always use the address the panel shows, without writing it by hand. The server accepts the connection on any path: you do not need to add anything after the address.
The message format
Every message is JSON text: an object with a type field that says which message it is. Messages are text only. Binary messages are ignored.
{ "type": "login", "identifier": "mario", "password": "la-tua-password" }The messages you send are from the client to the server. The ones you receive are from the server to the client.
The steps of a game
- Open the WebSocket connection.
- You send
registerto create a new account, orloginto get in with one that already exists. - The server answers with
welcome, to you only: inside there are your player, the other players already there and the level. The other players receiveplayer-joinedinstead. - You send the player inputs. How you do it depends on the movement mode of the game.
- You receive the messages about the other players: who joins, who moves, who leaves.
The protocol version
In the welcome message you find the protocolVersion field. Today it is 1.
The version changes only when a change breaks clients already written: removing or renaming a field, changing its type or its meaning. Adding new fields or new message types does not change it.
The server does not check the version of your client: the check is up to you. We suggest you do this:
- A field you do not know inside a message you know: ignore it. The server only added something.
- A message type you do not know: do not throw it away silently. At least write it in your game's log, so you notice the news.
protocolVersionmissing or different from the ones your client knows: stop and show a clear error.
What the server does with wrong messages
If you send a message that is not valid, or one that does not apply to the movement mode of the game, the server ignores it. It does not answer, and it usually writes a line in its log. Some cases are silent, for example inputs sent before the login. If something is not working, look first at the Server log in the panel.
- Text that is not valid JSON: thrown away, the connection stays open.
- A message without
type, or with atypethat is not a string: thrown away, the connection stays open. - A message of an unknown type: ignored.
The limits
- Size: a message can weigh at most 1 MiB (about one million bytes). If you go over it, the server closes the connection at once, without a closing message: your client sees code 1006.
- Connections: at most 10 connections open at the same time from the same address. Beyond that, the server refuses the connection with an HTTP 429 error, and the WebSocket connection does not open.
- Silence: the server sends no ping and never closes a connection because it is idle. It answers pings with a pong. If you need a check that the connection is alive, send it yourself.
- Logins and registrations: there are limits on attempts, explained in the Sign in and registration page.
The movement modes
A Nyra game moves in one of three ways, and whoever creates the game decides it:
- Tile: the character takes one step at a time, from one cell to the next.
- Platform: the character runs and jumps, with gravity.
- 8 directions: the character moves freely in pixels, in eight directions, or towards any angle.
Which mode the game uses you read in the movementMode field of the welcome message: it is tile, platform or eightdir. The game server uses one mode at a time. The messages to move are different in each mode: you find them in the Moving, by mode page.