Skip to content

Commit

Permalink
debug: Add debug logs to collab con
Browse files Browse the repository at this point in the history
  • Loading branch information
matevszm committed Nov 13, 2024
1 parent b752803 commit d4a6d1c
Showing 1 changed file with 75 additions and 61 deletions.
136 changes: 75 additions & 61 deletions src/hooks/useCollabSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,6 @@ export function useCollabSocket(
new Map()
);

useEffect(() => {
if (!socket && excalidrawApi) {
function onInitRoom() {
if (socket) socket.emit('join-room', roomId);
}

function onClientBroadcast(encryptedData: ArrayBuffer) {
const rawDecodedData = new TextDecoder('utf-8').decode(
new Uint8Array(encryptedData)
);
const decodedData: SocketUpdateData = JSON.parse(rawDecodedData);

switch (decodedData.type) {
case WS_SUBTYPES.MOUSE_LOCATION: {
const { pointer, button, username, selectedElementIds } =
decodedData.payload;

const socketId: SocketUpdateDataSource['MOUSE_LOCATION']['payload']['socketId'] =
decodedData.payload.socketId;

updateCollaborator(socketId, {
pointer,
button,
selectedElementIds,
username,
});

break;
}
case WS_SUBTYPES.UPDATE: {
reconcileAndUpdateCanvas(decodedData.payload.elements);
break;
}
default: {
console.log(`UNHANDLED EVENT: ${decodedData.type}`);
}
}
}

const socket = socketIOClient(import.meta.env.VITE_APP_WS_SERVER_URL, {
transports: ['websocket', 'polling'],
});

setSocket(socket);

if (socket && excalidrawApi) {
socket.on('init-room', onInitRoom);
socket.on('client-broadcast', onClientBroadcast);
}

return () => {
socket?.close();
setSocket(undefined);
socket?.off('init-room', onInitRoom);
socket?.off('client-broadcast', onClientBroadcast);
};
}
}, [excalidrawApi]);

const broadcastSocketData = useCallback(
(data: SocketUpdateData, volatile: boolean = false) => {
if (socket && roomId) {
Expand Down Expand Up @@ -181,10 +122,14 @@ export function useCollabSocket(
true // volatile
);
} else {
console.error('Failed attempt to broadcast mouse location');
console.error('Failed attempt to broadcast mouse location', {
socket,
excalidrawApi,
socketId: socket?.id,
});
}
},
[broadcastSocketData, excalidrawApi, socket?.id, user?.displayName]
[broadcastSocketData, excalidrawApi, socket, user?.displayName]
);

const broadcastCanvas = useCallback(
Expand Down Expand Up @@ -216,5 +161,74 @@ export function useCollabSocket(
[broadcastSocketData, syncElementVersions]
);

useEffect(() => {
if (!socket && excalidrawApi) {
function onInitRoom() {
if (socket) socket.emit('join-room', roomId);
}

function onClientBroadcast(encryptedData: ArrayBuffer) {
const rawDecodedData = new TextDecoder('utf-8').decode(
new Uint8Array(encryptedData)
);
const decodedData: SocketUpdateData = JSON.parse(rawDecodedData);

switch (decodedData.type) {
case WS_SUBTYPES.MOUSE_LOCATION: {
const { pointer, button, username, selectedElementIds } =
decodedData.payload;

const socketId: SocketUpdateDataSource['MOUSE_LOCATION']['payload']['socketId'] =
decodedData.payload.socketId;

updateCollaborator(socketId, {
pointer,
button,
selectedElementIds,
username,
});

break;
}
case WS_SUBTYPES.UPDATE: {
reconcileAndUpdateCanvas(decodedData.payload.elements);
break;
}
default: {
console.log(`UNHANDLED EVENT: ${decodedData.type}`);
}
}
}

console.log('env ws url: ', import.meta.env.VITE_APP_WS_SERVER_URL);

const socket = socketIOClient(import.meta.env.VITE_APP_WS_SERVER_URL, {
transports: ['websocket', 'polling'],
});

console.log({ socket });

setSocket(socket);

if (socket && excalidrawApi) {
socket.on('init-room', onInitRoom);
socket.on('client-broadcast', onClientBroadcast);
}

return () => {
socket?.close();
setSocket(undefined);
socket?.off('init-room', onInitRoom);
socket?.off('client-broadcast', onClientBroadcast);
};
}
}, [
excalidrawApi,
reconcileAndUpdateCanvas,
roomId,
socket,
updateCollaborator,
]);

return { socket, broadcastMouseLocation, broadcastCanvas };
}

0 comments on commit d4a6d1c

Please sign in to comment.