From 1903e3857025946bd435197bb1e5e1b994252fba Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 27 Nov 2023 15:48:15 +0000 Subject: [PATCH] feat: allow going live via websocket --- partykit/server.ts | 57 +++++++++++++------ .../layout/ThePresenceIndicator.vue | 10 +++- src/server/routes/v/[slug].ts | 6 +- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/partykit/server.ts b/partykit/server.ts index 7c75f6da..876a584a 100644 --- a/partykit/server.ts +++ b/partykit/server.ts @@ -2,37 +2,58 @@ import type { PartyKitServer, Party } from 'partykit/server' export default { async onConnect(ws, party) { - // 1. Handle live voting in slide deck - const set = async (val: number) => { + // 1. handle live voting in slide deck + const setCount = async (val: number) => { await party.storage.put('count', val) ws.send(`count:${val}`) } - ws.send(`count:${await get(party)}`) + ws.send(`count:${await getCount(party)}`) ws.addEventListener('message', async ({ data }) => { - if (data === 'vote') { - await set((await get(party)) + 1) - } - if (data === 'clear') { - await set(0) - } + if (data === 'vote') await setCount((await getCount(party)) + 1) + if (data === 'clear') await setCount(0) }) - // 2. Tell connectees how many people are viewing the site + // 2. let everyone know someone new is viewing the site party.broadcast(`connections:${[...party.getConnections()].length}`) + + // 3. let people know if I'm streaming + ws.send(`status:${(await party.storage.get('status')) || 'default'}`) }, - async onRequest(request, room) { - if (request.method === 'GET') { - return new Response(String(await get(room)), { status: 200 }) + async onRequest(request, party) { + if (request.method !== 'POST') + return new Response('Invalid request', { status: 422 }) + + const body = await request.text().then(r => (r ? JSON.parse(r) : {})) + const { status, type = status ? 'status' : 'vote' } = body as { + type?: 'vote' | 'status' + status?: string + } + + // 4. allow one-off live voting via link + if (type === 'vote') { + const val = (await getCount(party)) + 1 + await party.storage.put('count', val) + party.broadcast(`count:${val}`) + return new Response(null, { status: 204 }) } - if (request.method === 'POST') { - const val = (await get(room)) + 1 - await room.storage.put('count', val) - room.broadcast(`count:${val}`) + + // 5. tell people if I'm going live + if (type === 'status') { + if (!status || !['live', 'default'].includes(status)) + return new Response('Invalid status', { status: 422 }) + + if (request.headers.get('authorization') !== party.env.PARTYKIT_TOKEN) + return new Response('Unauthorised', { status: 401 }) + + party.storage.put('status', status) + party.broadcast(`status:${status}`) return new Response(null, { status: 204 }) } + return new Response('Invalid request', { status: 422 }) }, } satisfies PartyKitServer -const get = (room: Party) => room.storage.get('count').then(r => r || 0) +const getCount = (room: Party) => + room.storage.get('count').then(r => r || 0) diff --git a/src/components/layout/ThePresenceIndicator.vue b/src/components/layout/ThePresenceIndicator.vue index 3dd0d86e..e137e488 100644 --- a/src/components/layout/ThePresenceIndicator.vue +++ b/src/components/layout/ThePresenceIndicator.vue @@ -58,8 +58,14 @@ if (import.meta.client) { partySocket.onmessage = evt => { const data = evt.data as string const [type, value] = data.split(':') - if (type === 'connections') { - count.value = parseInt(value) + switch (type) { + case 'connections': + count.value = parseInt(value) + break + + case 'status': + if (value in colorSet) status.value = value as keyof typeof colorSet + break } } diff --git a/src/server/routes/v/[slug].ts b/src/server/routes/v/[slug].ts index c476c549..d204eeba 100644 --- a/src/server/routes/v/[slug].ts +++ b/src/server/routes/v/[slug].ts @@ -4,6 +4,10 @@ export default defineEventHandler(async event => { if (!slug || !/^[\da-z]+$/.test(slug)) { throw createError({ statusCode: 400, statusMessage: 'Missing slug' }) } - await $fetch(slug, { baseURL: config.voteUrl, method: 'POST' }) + await $fetch(slug, { + baseURL: config.voteUrl, + body: { type: 'vote' }, + method: 'POST', + }) return await sendRedirect(event, '/voted') }) -- knotmirror 0.0.1