mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-08-06 17:37:43 +00:00
fix: resolve ghost peer duplication and service worker state persistence issues
This commit is contained in:
+19
-2
@@ -18,9 +18,10 @@ let eventQueue = [];
|
||||
let isNamespaceJoined = false;
|
||||
|
||||
// Restore state from session storage
|
||||
chrome.storage.session.get(['logs', 'history'], (data) => {
|
||||
chrome.storage.session.get(['logs', 'history', 'currentRoom'], (data) => {
|
||||
if (data.logs) logs = data.logs;
|
||||
if (data.history) history = data.history;
|
||||
if (data.currentRoom) currentRoom = data.currentRoom;
|
||||
storageInitialized = true;
|
||||
|
||||
if (pendingLogs.length > 0) {
|
||||
@@ -314,6 +315,7 @@ function handleServerEvent(event, data) {
|
||||
switch (event) {
|
||||
case EVENTS.ROOM_DATA:
|
||||
currentRoom = data;
|
||||
if (storageInitialized) chrome.storage.session.set({ currentRoom });
|
||||
addLog(`Joined Room: ${data.roomId}`, 'success');
|
||||
chrome.runtime.sendMessage({ type: 'PEER_UPDATE', peers: data.peers }).catch(() => {});
|
||||
// Inform Website Bridge
|
||||
@@ -466,7 +468,21 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
if (message.type === 'CONNECT') {
|
||||
reconnectFailed = false;
|
||||
reconnectStartTime = null;
|
||||
connect();
|
||||
if (socket && socket.readyState === WebSocket.OPEN && isNamespaceJoined) {
|
||||
// Already connected, but maybe room changed or we need to refresh room state
|
||||
getSettings().then(settings => {
|
||||
if (settings.roomId) {
|
||||
emit(EVENTS.JOIN_ROOM, {
|
||||
roomId: settings.roomId,
|
||||
password: settings.password,
|
||||
peerId,
|
||||
protocolVersion: PROTOCOL_VERSION
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
connect();
|
||||
}
|
||||
} else if (message.type === 'RETRY_CONNECT') {
|
||||
reconnectFailed = false;
|
||||
reconnectStartTime = null;
|
||||
@@ -481,6 +497,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
} else if (message.type === 'LEAVE_ROOM') {
|
||||
emit(EVENTS.LEAVE_ROOM, { peerId });
|
||||
currentRoom = null;
|
||||
if (storageInitialized) chrome.storage.session.set({ currentRoom: null });
|
||||
addLog('Left Room', 'info');
|
||||
} else if (message.type === 'CLEAR_LOGS') {
|
||||
logs = [];
|
||||
|
||||
@@ -216,6 +216,21 @@ io.on('connection', (socket) => {
|
||||
socket.emit(EVENTS.ERROR, { message: "Room full" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Peer Deduplication: Remove existing socket for the same peerId
|
||||
for (const [sid, data] of room.peerData.entries()) {
|
||||
if (data.peerId === peerId && sid !== socket.id) {
|
||||
const oldSocket = io.sockets.sockets.get(sid);
|
||||
if (oldSocket) {
|
||||
oldSocket.leave(roomId);
|
||||
socketToRoom.delete(sid);
|
||||
}
|
||||
room.peers.delete(sid);
|
||||
room.peerIds.delete(sid);
|
||||
room.peerData.delete(sid);
|
||||
log('ROOM', `Deduplicated peer ${peerId} from room ${roomId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket.join(roomId);
|
||||
|
||||
Reference in New Issue
Block a user