Per device clientID

deno-express
Zef Hemel 2023-05-30 14:48:49 +02:00
parent b3876e80b6
commit bdf9a16c50
1 changed files with 17 additions and 4 deletions

View File

@ -162,7 +162,16 @@ export function writeFileCollab(name: string): FileMeta {
}; };
} }
const clientId = nanoid(); // Generate a random client ID and store it in the store
// clientIDs will be unique per device
const clientId = store.get("collabClientId").then(async (clientId) => {
if (!clientId) {
clientId = nanoid();
await store.set("collabClientId", clientId);
}
return clientId;
});
let currentCollabId: string | undefined; let currentCollabId: string | undefined;
const localCollabServer = location.protocol === "http:" const localCollabServer = location.protocol === "http:"
@ -173,20 +182,24 @@ async function ping() {
try { try {
const currentPage = await editor.getCurrentPage(); const currentPage = await editor.getCurrentPage();
const { collabId } = await collab.ping( const { collabId } = await collab.ping(
clientId, await clientId,
currentPage, currentPage,
); );
console.log("Collab ID", collabId); console.log("Collab ID", collabId);
if (!collabId && currentCollabId) { if (!collabId && currentCollabId) {
// Stop collab // Stop collab
console.log("Stopping collab"); console.log("Stopping collab");
// editor.flashNotification("Closing real-time collaboration mode."); editor.flashNotification(
"Other users have left this page, switched back to single-user mode.",
);
currentCollabId = undefined; currentCollabId = undefined;
await collab.stop(); await collab.stop();
} else if (collabId && collabId !== currentCollabId) { } else if (collabId && collabId !== currentCollabId) {
// Start collab // Start collab
console.log("Starting collab"); console.log("Starting collab");
editor.flashNotification("Opening page in real-time collaboration mode."); editor.flashNotification(
"Another device has joined this page, switched to multi-user mode.",
);
currentCollabId = collabId; currentCollabId = collabId;
await collab.start( await collab.start(
localCollabServer, localCollabServer,