fix: Send join message after WebSocket connects

The join() was called before connect(), so the join message was never
sent to the server. Now we store the participant and send the join
message in the WebSocket onopen handler.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2025-12-26 20:39:44 -05:00
parent 8554a4cb1a
commit 2d960a53f2
1 changed files with 11 additions and 2 deletions

View File

@ -89,6 +89,7 @@ export class RoomSync {
private onStateChange: SyncCallback;
private onConnectionChange: ConnectionCallback;
private participantId: string;
private currentParticipant: ParticipantState | null = null;
constructor(
slug: string,
@ -187,8 +188,12 @@ export class RoomSync {
this.ws.onopen = () => {
console.log('Connected to sync server');
this.onConnectionChange(true);
// Request current state from server
this.send({ type: 'request_state' });
// Send join message if we have participant info
if (this.currentParticipant) {
console.log('Sending join message for:', this.currentParticipant.name);
this.send({ type: 'join', participant: this.currentParticipant });
}
};
this.ws.onmessage = (event) => {
@ -280,7 +285,11 @@ export class RoomSync {
// Public methods for updating state
join(participant: ParticipantState): void {
// Store participant for sending after WebSocket connects
this.currentParticipant = participant;
this.state.participants[participant.id] = participant;
// Try to send join (will succeed if already connected)
this.send({ type: 'join', participant });
this.notifyStateChange();
}