Sure -

Sure - The Durable Object handles websocket clients for a multiplayer game using a deterministic physics engine. The physics are executed on both the clients and the server. Serializing the physics world costs up to a few KB per player. To join games, I have a lobbies table in D1 with a game_state column: 'waiting' | 'playing' | 'finished'. I need to set the game to the finished state when it's abandoned, so if the player rejoins the websocket later, it won't re-initialize the DO. Because the transactional storage API is so expensive, I want to treat crashed games as permanently dead and show clients an error when they reconnect.
E
Erwin196d ago
Moving this to a thread btw.. keeps the channel cleaner for other people
O
Ottomated196d ago
👍 Hmm. I do already have a tick_index that gets incremented, so if the client detects that it's decreased it knows it's broken? seems really hard to get right
E
Erwin196d ago
I mean when the DO is reinitialised, it wont have any state.. so that might be a clue? When the DO gets a regular gaming message when it doesn't have any state, it knows something is up I would imagine?
O
Ottomated196d ago
that might work But then I am letting my clients dictate when I've crashed Also they don't send heartbeats But I guess they would auto-reconnect... or maybe not? because if I'm using the Hibernation API, when the DO crashes they would stay connected or is that wrong? I guess the foolproof solution is to store some "game_started" boolean in transactional storage, and then if that's set to true when the DO is constructed I know it's in a bad state However, the flaw with that solution is that I only detect the crash once the clients reconnect - it doesn't detect abandonment
E
Erwin196d ago
The clients would try to reconnect yes. So you would receive a message. Now I don't know your protocol obviously, but I can imagine that the messages you receive during a game are different than before a game?
O
Ottomated196d ago
My only concern is a malicious client here.
E
Erwin196d ago
Just kill that client? And if all connections are killed clean up and let the DO die? I am not sure if you can set a timeout on the hibernation API? I haven't used it myself.. but otherwise you will want to use an alarm for that
O
Ottomated196d ago
I mean if my metric for detecting a crash is if a client sends the wrong type of message, a malicious client could send that message intentionally and kill the DO
E
Erwin196d ago
Don't kill the DO.. kill the client connection.
O
Ottomated196d ago
I need to write to D1 that the lobby is dead
E
Erwin196d ago
And if all connections are gone, the DO will die
O
Ottomated196d ago
I guess that would work, but now we're back to needing a way to detect when the DO dies naturally
E
Erwin196d ago
You mean when all clients stop playing? Hmm.. so I just realised that in your case the DO can still be unloaded, even though gamers are connected right?
O
Ottomated196d ago
There are 3 cases for the DO dying: 1. Game ends, I kick all clients and update D1 2. Game is abandoned, all clients are disconnected - I guess I do this on a close event + a timer? Still impractical 3. DO crashes, client almost certainly instantly auto-reconnects, I detect that the DO is being initialized in a bad state and update D1 I only want to hibernate in the waiting state - during the playing state I'm broadcasting 10 ticks per second
E
Erwin196d ago
ok.. that makes sense. So how long can the waiting state take?
O
Ottomated196d ago
Until the lobby fills
Want results from more Discord servers?
Add your server
More Posts
Still stuck behind 1mb script size limit after upgrading to paid planI upgraded to a paid plan yesterday but I'm still seeing the message `Error: Failed to publish your Thanks for responding la x rc 1203 sorryThanks for responding @la.x.rc , sorry I missed it. Should I follow up with a forum thread instead? Stack Suggestions!!!For a web app with the following functionality: - User authentication with an auth provider - FetchiAny way around the 20k files limit for Pages?We have a website that uses Next.js static rendering . We generate at the moment around 10k pages anIt appears that a `Promise.all()` of "pg" requests happens serially rather than parallel. Expected?Hi there, I'm using the `pg` package inside a cloudflare worker. It's connected via a socket to a Neanyone can help me with cloudflare pages i am trying to add CNAME but getting error inactive(error)How to deal with Pages/Workers outages?It seems there were issues for pages/workers yersterday. What is the best way we can deal with thoCan't access KV namespace in local dev [VITE]I'm pretty sure I'm entering the build command wrong. This will build the project just fine: `npx wHow to redirect requests based on geolocation on a Shopify siteHi, I'm trying to run a redirect worker on my site that serve a Shopify site. It has A and CNAME recdownlowding cloudflare pages site not on githow to downlowd cloudflare pages site not on gitHow to Cache API response, with Hono JS & workersHello all newbie here, I am making an API where in an endpoint I am calling a third party API and seLatency Cloudflare Workers + Workers KVHello guys. Just doing a few tests and the latency is around 110-200ms after a cold start. I'm pingEmail RoutingI thought the point of email routing was to be a sort of middleman between the sender and recipient?Hi all, can anybody help me with addingHi all, can anybody help me with adding custom domain to R2. It stucks with status "Unknown"Can I use cloudflare image to optimize images stored in s3?without upload to image endpoint first? or is there any best practice to build a workflow to triggerGithub Integration Deployment EnvironmentsHi, Relating to deployment id `3faaa302-9ce4-4f69-a7a0-d923e8a698c6` account id `26968497a25eabba33how to handle dependencies using node built-insHey! I'm new to Cloudflare, and trying to convert my node server to work in Cloudflare workers insteSeems like https://r2.dev has beenSeems like https://r2.dev has been blocked AGAIN by Korean GovermentGitHub - windwp/kysely-zod-sqlite: An fl...i use my library to unit test on local with better sqlite https://github.com/windwp/kysely-zod-sqlitSegfaulting when building my Gatsby projectI constantly have to restart builds on pages and tweaked all of the configurable settings. Any other