fix: Chats doesn't sync for multi client users

This commit is contained in:
2026-06-11 00:04:49 +02:00
parent d0aff6319d
commit d174536272
16 changed files with 662 additions and 16 deletions

View File

@@ -705,7 +705,32 @@ async function handlePluginEvent(user: ConnectedUser, message: WsMessage, connec
}
}
export async function handleWebSocketMessage(connectionId: string, message: WsMessage): Promise<void> {
/**
* Tail of the in-flight message chain per connection.
*
* Messages from one client can arrive in the same tick (one TCP segment), but
* handlers like identify await async work. Without serialization a
* join_server can be evaluated while identify is still pending, get rejected
* as unauthenticated, and silently lose the room membership.
*/
const connectionMessageChains = new Map<string, Promise<void>>();
export function handleWebSocketMessage(connectionId: string, message: WsMessage): Promise<void> {
const prior = connectionMessageChains.get(connectionId) ?? Promise.resolve();
const current = prior.then(() => processWebSocketMessage(connectionId, message));
const tail = current.catch(() => undefined);
connectionMessageChains.set(connectionId, tail);
void tail.then(() => {
if (connectionMessageChains.get(connectionId) === tail) {
connectionMessageChains.delete(connectionId);
}
});
return current;
}
async function processWebSocketMessage(connectionId: string, message: WsMessage): Promise<void> {
const user = connectedUsers.get(connectionId);
if (!user)