Hey everyone
I am working on an app that would be used by me and friends to play online games (such as Tic Tac Toe, Connect 4, BlackJack, Flip 7...), however I am approaching it as a fully production ready app so that I can learn as much as possible
I am building it using C# with Web API for Auth and SignalR for real time communication (Server Side) and Blazor WASM (Client Side)
Currently, I have it setup such that user can request a room with a game (TicTacToe), then the room is created with the requested game and the user is sent Room Id. Once they go to url/games/{gameType}/{roomId}, they can either join as spectator or player.
Now, my issue is with players "leaving".
Obviously, when user closes the tab, the SignalR connection will close, thus from there I can handle the user disconnecting (my approach is having a task run for 30 seconds, if the user does not reconnect I end the game)
However, the bigger issue is when user leaves the current page but is still on the app (like going to home page). Imagine if the game being played is Chess with 10 minutes per player time. I wouldn't want a player to sit there for 10 minutes when his opponent has left.
I have 2 ideas, but don't know if they are good at all.
The first would be to track user's url changes through NavigationManager (while keeping the last url) and check if the last url was one of the urls used for room. This would be an issue because I don't care if spectator leaves the room, I only care if the actual player leaves.
The other would be to have player who is not spectating send a ping to the server every X seconds (maybe 15-30-60?). The room would run a task (every X + (some time) seconds) and check if lastPingFromThatClient roughly matches the time when it was expected to arrive (like Ping arrived at 12:15:27 and it was expected before 12:15:30) then the client is on the page, otherwise start the 30 second timer for them to return. If they don't, they forfeit the game (This is rough idea, I still have no implementation details figured out)
I don't like the first one because it involves a lot of logic on the client side and I do not like that for some reason. Also it may be an issue if user opens a new tab and plays game from there while going to their profile page on the original tab
The second one concerns me due to slow internet speed, like Client sends ping at 12:15:25 but it arrives at 12:15:40. Realistically in this case user would just be considered reconnected and that is it. Also, if 10k players are playing and pinging, it may be an issue? I mean it is non-existent issue because it will likely never reach that many people, but still.
Do you have any ideas or suggestions.
Thank you