Allow configuring server port

This commit is contained in:
arne 2024-03-21 00:53:46 +01:00
commit bc9ef895ad

View file

@ -9,60 +9,64 @@ type Message =
const clients: Map<WebsocketData['clientId'], WebSocket> = new Map()
const _server = Deno.serve((req) => {
const _server = Deno.serve({
port: Number.parseInt(Deno.env.get('PORT') || '8000', 10),
handler: (req) => {
if (req.headers.get("upgrade") !== "websocket") {
return new Response(null, { status: 501 })
return new Response(null, { status: 501 })
}
const { socket, response } = Deno.upgradeWebSocket(req)
// TODO Allow creating private ponds
const clientId = crypto.randomUUID()
console.log('generated clientId', clientId)
socket.addEventListener("open", () => {
// register newly connected client and tell them how many other people are there
console.log('Connection opened', clientId, clients)
socket.send(JSON.stringify(<Message>{
type: 'welcome',
id: clientId
}))
const enterNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size,
})
clients.set(clientId, socket)
for (const client of clients.values()) {
client.send(enterNotice)
}
})
socket.addEventListener("message", (event) => {
const message = `${event.data}`
// broadcast message to all other clients
// TODO: Validate message shape
const msg = JSON.parse(`${message}`)
console.log('Relaying message from', clientId, msg)
for (const [uuid, client] of clients.entries()) {
if (uuid !== clientId) {
client.send(message)
}
}
})
socket.addEventListener("close", () => {
// remove client from list of registered clients and tell other clients how many people are there
console.log('Connection closed', clientId, clients)
clients.delete(clientId)
const leaveNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size - 1,
})
for (const [uuid, client] of clients.entries()) {
if (uuid !== clientId) {
client.send(leaveNotice)
}
}
})
return response
}
const { socket, response } = Deno.upgradeWebSocket(req)
// TODO Allow creating private ponds
const clientId = crypto.randomUUID()
console.log('generated clientId', clientId)
socket.addEventListener("open", () => {
// register newly connected client and tell them how many other people are there
console.log('Connection opened', clientId, clients)
socket.send(JSON.stringify(<Message>{
type: 'welcome',
id: clientId
}))
const enterNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size,
})
clients.set(clientId, socket)
for (const client of clients.values()) {
client.send(enterNotice)
}
})
socket.addEventListener("message", (event) => {
const message = `${event.data}`
// broadcast message to all other clients
// TODO: Validate message shape
const msg = JSON.parse(`${message}`)
console.log('Relaying message from', clientId, msg)
for (const [uuid, client] of clients.entries()) {
if (uuid !== clientId) {
client.send(message)
}
}
})
socket.addEventListener("close", () => {
// remove client from list of registered clients and tell other clients how many people are there
console.log('Connection closed', clientId, clients)
clients.delete(clientId)
const leaveNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size - 1,
})
for (const [uuid, client] of clients.entries()) {
if (uuid !== clientId) {
client.send(leaveNotice)
}
}
})
return response
})