Server has to be as stateless as possible.
- Websockets are stateful but http calls are stateless
- Keep the Websocket as slim as possible
- One Websocket per user is enough
- Once a message goes in, en-route to where it belongs
- Say you will have classes for each game
- Don't refresh the page everytime the user navigates
- @aeric already has a couple of examples with HTMX
- HTMX WS is worth your while.
The clients will be Android and iOS, so at least for now no html, no html pages.
The slim websocket... does not depend on me. Maybe you were referring to the class that manages the websocket; this can be more or less large, it depends on the organization/structure of everything and is the basis of my question, which evidently I did not explain well.
Of course I will have a websocket for client, but I had a different way to develop the project, compared to the initial idea, kept for a long time; I try to explain it again.
"Concept" always had:
srvr.AddWebSocket("/Test", "wshUser")
The wshUser class represents user-client. Then I would have "normal" classes depending on the type of game chosen; clsChessPlayer would be specific for the chess game and I would pass it the wshUser object, only for the main user data (an ID and little else) and for client-server communications.
"New concept", recent:
srvr.AddWebSocket("/Test/Chess", "wshChessPlayer")
srvr.AddWebSocket("/Test/BlackJack", "wshBlackJackPlayer")
After authentication, which should happen in wshUser, as above, when the user chooses a game, the client disconnects from the current websocket and creates another one, to connect to one of the paths above.
In practice, I could choose whether to have a class that generically represents a user and multiple player classes specific to each game to which I would pass the wshUser object or each player class would actually be a websocket handler with the rules for the player.
As for database access, I think I'll do it as little as possible; I'll have thread safe maps for rooms and players but I won't write to DB everything that happens in the rooms, every move of the players, the game, player and room state. I would save the player's state to DB in case of disconnection (but I will have to foresee a way to delete this if the user will not reconnect soon; probably I will do it at the end of the game).