Set up server to relay ripples and presence info

This commit is contained in:
arne 2024-01-27 12:04:46 +01:00
commit 8ccb60de61
7 changed files with 338 additions and 7 deletions

175
server/.gitignore vendored Normal file
View file

@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

15
server/README.md Normal file
View file

@ -0,0 +1,15 @@
# @compost.party/pond-server
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.0.24. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

BIN
server/bun.lockb Executable file

Binary file not shown.

65
server/index.ts Normal file
View file

@ -0,0 +1,65 @@
import { type ServerWebSocket } from "bun"
type WebsocketData = {
clientId: string
}
type Message =
| { type: 'presence-information', others: number }
| { type: 'ripple', position: [number, number], maxRadius: number }
const clients: Map<WebsocketData['clientId'], ServerWebSocket<WebsocketData>> = new Map()
const server = Bun.serve<WebsocketData>({
fetch(req, server) {
// TODO Allow creating private ponds
server.upgrade(req, {
data: {
clientId: crypto.randomUUID()
}
})
},
websocket: {
open(ws) {
// register newly connected client and tell them how many other people are there
console.log('Connection opened', ws.data.clientId)
clients.set(ws.data.clientId, ws)
const enterNotice = JSON.stringify(<Message>{
type: 'presence-information',
others: clients.size,
})
for (const [uuid, client] of clients.entries()) {
if (uuid !== ws.data.clientId) {
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)
}
}
}
},
})
console.log(`Server running on ${server.hostname}:${server.port}`)

11
server/package.json Normal file
View file

@ -0,0 +1,11 @@
{
"name": "@compost.party/pond-server",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}

22
server/tsconfig.json Normal file
View file

@ -0,0 +1,22 @@
{
"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
}
}