switch to deno

This commit is contained in:
arne 2024-03-20 23:55:41 +01:00
commit a7bbe6180d
9 changed files with 304 additions and 97 deletions

Binary file not shown.

1
server/deno.json Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -1,63 +1,68 @@
import { type ServerWebSocket } from "bun"
type WebsocketData = {
clientId: string
}
type Message =
| { type: 'welcome', id: string }
| { type: 'presence-information', others: number }
| { type: 'ripple', position: [number, number], maxRadius: number }
const clients: Map<WebsocketData['clientId'], ServerWebSocket<WebsocketData>> = new Map()
const clients: Map<WebsocketData['clientId'], WebSocket> = new Map()
const server = Bun.serve<WebsocketData>({
fetch(req, server) {
// TODO Allow creating private ponds
server.upgrade(req, {
data: {
clientId: crypto.randomUUID()
}
const _server = Deno.serve((req) => {
if (req.headers.get("upgrade") !== "websocket") {
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,
})
},
websocket: {
open(ws) {
// register newly connected client and tell them how many other people are there
console.log('Connection opened', ws.data.clientId)
const enterNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size,
})
clients.set(ws.data.clientId, ws)
for (const client of clients.values()) {
client.send(enterNotice, true)
}
},
message(ws, message) {
// broadcast message to all other clients
// TODO: Validate message shape
const msg = JSON.parse(`${message}`)
console.log('Relaying message from', ws.data.clientId, msg)
for (const [uuid, client] of clients.entries()) {
if (uuid !== ws.data.clientId) {
client.send(message)
}
}
},
close(ws, code, reason) {
// remove client from list of registered clients and tell other clients how many people are there
console.log('Connection closed', ws.data.clientId)
clients.delete(ws.data.clientId)
const leaveNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size,
})
for (const [uuid, client] of clients.entries()) {
if (uuid !== ws.data.clientId) {
client.send(leaveNotice, true)
}
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)
}
}
},
})
})
console.log(`Server running on ${server.hostname}:${server.port}`)
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
})

View file

@ -1,22 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
/* Linting */
"skipLibCheck": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}