fix: Chats doesn't sync for multi client users
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user