reconexion basada en el UUID

This commit is contained in:
2025-08-15 18:51:46 -06:00
parent 811f569391
commit 310fb3455a
6 changed files with 176 additions and 2 deletions

View File

@@ -139,6 +139,8 @@ class ColyseusService {
// Update current session id for correct role mapping
try { this.sessionId.value = (gameRoom as any).sessionId || this.sessionId.value; } catch {}
console.log('gameRoom.value is now:', this.gameRoom.value);
// Register reconnection token on server for this UUID
try { (gameRoom as any).send("registerReconnection", (gameRoom as any).reconnectionToken || ""); } catch {}
// Don't register message handlers here - let the Game component handle them
@@ -172,6 +174,8 @@ class ColyseusService {
try { this.sessionId.value = (gameRoom as any).sessionId || this.sessionId.value; } catch {}
this.gameRoom.value = gameRoom;
this.currentRoom = gameRoom;
// Register reconnection token on server for this UUID
try { (gameRoom as any).send("registerReconnection", (gameRoom as any).reconnectionToken || ""); } catch {}
// Don't register message handlers here - let the Game component handle them
@@ -316,6 +320,17 @@ class ColyseusService {
}
}
// Used when lobby provides a reconnection token to resume a locked room
async reconnectWithToken(token: string): Promise<Room> {
const room = await (this.client as any).reconnect(token);
this.gameRoom.value = room;
this.currentRoom = room;
try { this.sessionId.value = (room as any).sessionId || this.sessionId.value; } catch {}
// Refresh reconnection token mapping on server
try { (room as any).send("registerReconnection", (room as any).reconnectionToken || ""); } catch {}
return room;
}
private getUuidFromPath(): string {
if (typeof window === 'undefined') return '';
const path = window.location.pathname.replace(/^\/+/, '');

View File

@@ -115,6 +115,36 @@ onMounted(async () => {
const room = await colyseusService.joinLobby();
colorInput.value = colyseusService.playerColor.value || '#667eea';
// Prefer reconnection token path to bypass locked rooms
room.onMessage("resumeReconnection", async (data: any) => {
try {
await colyseusService.reconnectWithToken(data.token);
// Leave lobby before navigating
if (colyseusService.lobbyRoom.value) {
colyseusService.lobbyRoom.value.leave();
colyseusService.lobbyRoom.value = null;
}
await router.push(`/${routeUuid.value}/demo`);
} catch (error) {
console.error('Reconnection failed:', error);
}
});
// Listen for server-initiated resume to existing game (fallback joinById)
room.onMessage("resumeGame", async (data: any) => {
try {
const gameRoom = await colyseusService.joinGameRoom(data.roomId);
// Leave lobby before navigating
if (colyseusService.lobbyRoom.value) {
colyseusService.lobbyRoom.value.leave();
colyseusService.lobbyRoom.value = null;
}
await router.push(`/${routeUuid.value}/demo`);
} catch (error) {
console.error('Auto-join failed:', error);
}
});
// Keep color input synced with server-updated color
watch(() => colyseusService.playerColor.value, (c) => {
if (c && c !== colorInput.value) colorInput.value = c;