The chess server uses JSON transmitted over WebSockets for communication. The general structure of these JSON strings is as follows:
type: The type of packet being sent. This is a string that identifies the packet type.data: The data of the packet. This is a JSON object that contains the data for the packet.
Client A Server Client B
| | |
| LoginC2S | |
| >>>>>>>>>>>>>> | |
| | |
| LoginResultS2C | |
| <<<<<<<<<<<<<< | |
| | |
| UserListS2C | UserListS2C |
| <<<<<<<<<<<<<< | >>>>>>>>>>>>>> |
- On connect, the client sends a login packet to the server. This allows the server to test the validity of the client, as well as allowing the client to declare metadata about itself, such as the username.
- After validating the login packet, the server sends an acknowledgement packet back to the client. If the login was successful, the new user list is transmitted to all connected clients.
The server will ignore any other packets from the client until this entire exchange has been completed, and may kick the client if any other packet is received during this stage.
Packets:
When the server kicks a client, it may optionally send a KickS2C packet immediately before closing the connection, which
contains the reason why the client was kicked (so the client can display it to the user). This is not required though -
the server may simply terminate the connection. After the connection is closed, the server should send the new user list
to the other connected clients.
Packets:
Client A Server Client B
| | |
| CreateGameC2S | |
| >>>>>>>>>>>>>>>>>>> | |
| | |
| CreateGameResultS2C | |
| <<<<<<<<<<<<<<<<<<< | |
| | |
| | InviteS2C |
| | >>>>>>>>>>>>>>>>> |
| | |
| | InviteResponseC2S |
| | <<<<<<<<<<<<<<<< |
| | |
| UserJoinedS2C | JoinGameS2C |
| <<<<<<<<<<<<<<<<<<< | >>>>>>>>>>>>>>>>> |
| | |
| StartGameS2C | StartGameS2C |
| <<<<<<<<<<<<<<<<<<< | >>>>>>>>>>>>>>>>> |
- The client sends a
CreateGameC2Spacket to the server, requesting to create a game with a set of parameters. - The server validates the request and sends a
CreateGameResultS2Cpacket back to the client, containing the result of the request. On success, the game is created and the client is added to the game, which gets transmitted as part of the result packet. - The server sends an
InviteS2Cpacket to all invited clients, notifying them of the new game and allowing them to join it. - The invited clients respond with an
InviteResponseC2Spacket, indicating whether they accept or decline the invitation. - All clients that accepted the invitation are added to the game and get sent a
JoinGameS2Cpacket, and all clients already in the game are notified of the new user that joined. - Once all required clients have joined, the server sends a
StartGameS2Cpacket to all clients in the game, notifying them that the game has started.
Packets:
CreateGameC2S: example, schemaCreateGameResultS2C: example, schemaInviteS2C: example, schemaInviteResponseC2S: example, schemaJoinGameS2C: example, schemaUserJoinedS2C: example, schemaStartGameS2C: example, schema
Client A Server Client B
| | |
| LeaveGameC2S | |
| >>>>>>>>>>>>>>>>> | |
| | |
| | UserLeftS2C |
| | >>>>>>>>>>>>>>>>> |
- The client sends a
LeaveGameC2Spacket to the server, requesting to leave the game. - The server removes the client from the game and sends a
UserLeftS2Cpacket to all other clients in the game, notifying them of the user that left.
Packets:
Client A Server Client B
| | |
| RequestMovesC2S | |
| >>>>>>>>>>>>>>> | |
| | |
| MovesS2C | |
| <<<<<<<<<<<<<<< | |
| | |
| MoveC2S | |
| >>>>>>>>>>>>>>> | |
| | |
| MoveResultS2C | MoveS2C |
| <<<<<<<<<<<<<<< | >>>>>>>>>>>>> |
- The client sends a
RequestMovesC2Spacket to the server, requesting the list of possible moves for a piece. - The server sends a
SendMovesS2Cpacket back to the client, containing the list of possible moves for the piece. - The client sends a
MoveC2Spacket to the server, containing the move to be made. - The server validates the move and sends a
MoveResultS2Cpacket back to the client, containing the result of the move, or an error message if the move was invalid. - The server sends a
MoveS2Cpacket to all clients in the game, notifying them of the move that was made.
Packets:
{ "type": "...", "data": { // packet data } }