Persisting a socket on disconnect until a condition is met? #4208
Replies: 1 comment 1 reply
-
This is the pattern I ended up using: The server has a "post office", and when a user creates a game their socket is given an entry in the post office. Each game that user creates has its own entry as well (because the user might start a new game while the old game is still sending messages). The function of the post office is to hold a queue of messages to send, and then run a daemon that occasionally checks for the queue new messages (using setInterval). This continues until the socket's disconnect message tells the post office their socket is no longer active. Then the post office will teardown the entries for that socket's games once their message queues are empty. When all of an inactive socket's queues are emptied it's scrubbed from the post office.
Helpers:
However, the teardown aspect still doesn't feel very eloquent. In order to fully remove a player from the server once all their messages are sent, the post office has to be checked using a nested for loop. I don't think there will be more than a few hundred or so players on a single server though, and each player likely won't have more than one game going for very long. The teardown of the games as opposed to the socket is also desegmented so that there won't be orphaned daemons checking empty queues for a players past games. I think I will continue to refactor this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello~
As part of an app I'm writing, each connected user's socket has an array of tasks to perform. Every few milliseconds, this array is accessed by a coroutine that runs server-side. When a user disconnects from the server, I'd like their socket to persist until this array is empty.
Is there some way to keep a user's socket alive until a condition is met? In this case: until their socket's array is length 0?
Thanks.
Edit: Or, if there's better architecture for this sort of thing feel free to redirect.
Beta Was this translation helpful? Give feedback.
All reactions