Avoid duplicate sessions across devices/browsers
This commit is contained in:
parent
1ab7d27ba9
commit
81a763f17f
1 changed files with 22 additions and 8 deletions
|
@ -182,12 +182,21 @@ export function ClientProvider({ children }) {
|
||||||
}, [history]);
|
}, [history]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if ("BroadcastChannel" in window) {
|
if (client) {
|
||||||
const loadTime = Date.now();
|
const loadTime = Date.now();
|
||||||
const broadcastChannel = new BroadcastChannel("matrix-video-chat");
|
|
||||||
|
|
||||||
function onMessage({ data }) {
|
const onToDeviceEvent = (event) => {
|
||||||
if (data.load !== undefined && data.load > loadTime) {
|
if (event.getType() !== "org.matrix.call_duplicate_session") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const content = event.getContent();
|
||||||
|
|
||||||
|
if (content.session_id === client.getSessionId()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content.timestamp > loadTime) {
|
||||||
if (client) {
|
if (client) {
|
||||||
client.stopClient();
|
client.stopClient();
|
||||||
}
|
}
|
||||||
|
@ -199,13 +208,18 @@ export function ClientProvider({ children }) {
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
broadcastChannel.addEventListener("message", onMessage);
|
client.on("toDeviceEvent", onToDeviceEvent);
|
||||||
broadcastChannel.postMessage({ load: loadTime });
|
|
||||||
|
client.sendToDevice("org.matrix.call_duplicate_session", {
|
||||||
|
[client.getUserId()]: {
|
||||||
|
"*": { session_id: client.getSessionId(), timestamp: loadTime },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
broadcastChannel.removeEventListener("message", onMessage);
|
client.removeListener("toDeviceEvent", onToDeviceEvent);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [client]);
|
}, [client]);
|
||||||
|
|
Loading…
Reference in a new issue