Add implementation
This commit is contained in:
22
src/lib/peer.ts
Normal file
22
src/lib/peer.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
const PEER_ID_KEY = "p2p-poll-peer-id";
|
||||
|
||||
/** Generate a UUID v4 */
|
||||
function generateUUID(): string {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create a persistent peer ID.
|
||||
* Stored in localStorage so each browser tab/device gets a stable identity.
|
||||
*/
|
||||
export function getPeerId(): string {
|
||||
if (typeof globalThis.localStorage === "undefined") {
|
||||
return generateUUID();
|
||||
}
|
||||
let id = localStorage.getItem(PEER_ID_KEY);
|
||||
if (!id) {
|
||||
id = generateUUID();
|
||||
localStorage.setItem(PEER_ID_KEY, id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
Reference in New Issue
Block a user