dimanche 3 juin 2018

Async Callbacks Through Shared Message Queue

I am creating a web game that connects via websockets.

It's easy enough to create a messaging system that handles calls in a websocket. The challenge is call response. "Request World Chunk 0,0" is easy enough. What I need is a way to distinguish message returns. I send a data request to the server, the next message I get back through the websocket could be anything: a player action, a score change, physics sync data, etc.

The best solution I can come up with is generate a UUID for the call, have that UUID be part of the return data, and then run the callback (or promise) for that UUID. (ie: {request:'Get 0,0',id:'1234'} {response:DATA,id:'1234'}). Then I need to track all those promises on the requester side, and manage some sort of timeout system.

I'm trying to find a better design pattern for this: A way to asynchronously callback through a single line of communication. Is there a better design for this than a callback ID system? Is there a Browser/Nodejs package that handles this easily already?

note: I mention websockets here, but I also expect a similar problem/solution for webworker elements: "create physics object", but the next message from the physics thread might be a collision message or renderer update message.

Aucun commentaire:

Enregistrer un commentaire