Promote active screen shares
This commit is contained in:
@@ -8,6 +8,7 @@ const host = process.env.HOST || "127.0.0.1";
|
||||
const publicDir = join(process.cwd(), "public");
|
||||
const rooms = new Map();
|
||||
const watchStates = new Map();
|
||||
const presentationStates = new Map();
|
||||
const defaultRoomId = "main";
|
||||
|
||||
const mimeTypes = {
|
||||
@@ -76,10 +77,15 @@ function removePeer(roomId, peerId) {
|
||||
|
||||
room.delete(peerId);
|
||||
broadcast(roomId, peerId, { type: "peer-left", peerId });
|
||||
if (presentationStates.get(roomId)?.peerId === peerId) {
|
||||
presentationStates.delete(roomId);
|
||||
broadcast(roomId, peerId, { type: "presentation-state", from: peerId, sharing: false });
|
||||
}
|
||||
|
||||
if (room.size === 0) {
|
||||
rooms.delete(roomId);
|
||||
watchStates.delete(roomId);
|
||||
presentationStates.delete(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +146,8 @@ const server = http.createServer(async (req, res) => {
|
||||
peerId,
|
||||
name: displayName,
|
||||
peers: existingPeers,
|
||||
watchState: watchStates.get(roomId) || null
|
||||
watchState: watchStates.get(roomId) || null,
|
||||
presentationState: presentationStates.get(roomId) || null
|
||||
})}\n\n`
|
||||
);
|
||||
broadcast(roomId, peerId, { type: "peer-joined", peerId, name: displayName });
|
||||
@@ -156,6 +163,34 @@ const server = http.createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "POST" && url.pathname.startsWith("/room-event/")) {
|
||||
const roomId = url.pathname.split("/").at(-1) || defaultRoomId;
|
||||
const payload = await parseJson(req);
|
||||
|
||||
if (!payload.from || payload.type !== "presentation-state") {
|
||||
sendJson(res, 400, { error: "Room events require from and type=presentation-state." });
|
||||
return;
|
||||
}
|
||||
|
||||
const event = {
|
||||
type: "presentation-state",
|
||||
from: payload.from,
|
||||
name: normalizeName(payload.name),
|
||||
sharing: Boolean(payload.sharing),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
if (event.sharing) {
|
||||
presentationStates.set(roomId, { ...event, peerId: payload.from });
|
||||
} else if (presentationStates.get(roomId)?.peerId === payload.from) {
|
||||
presentationStates.delete(roomId);
|
||||
}
|
||||
|
||||
broadcast(roomId, payload.from, event);
|
||||
sendJson(res, 202, { ok: true });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "POST" && url.pathname.startsWith("/signal/")) {
|
||||
const roomId = url.pathname.split("/").at(-1);
|
||||
const payload = await parseJson(req);
|
||||
|
||||
Reference in New Issue
Block a user