send subdomain on success

This commit is contained in:
Chris Wanstrath 2025-09-23 21:02:29 -07:00
parent 3d630ed1f2
commit 994fc730c5

View File

@ -18,15 +18,14 @@ type Response = {
body: string body: string
} }
type Success = {
subdomain: string
}
type Connection = { app: string, ws: any } type Connection = { app: string, ws: any }
let connections: Record<string, Connection> = {} let connections: Record<string, Connection> = {}
const pending = new Map<string, (res: any) => void> const pending = new Map<string, (res: any) => void>
function send(connection: any, msg: Request) {
console.log("sending", msg)
connection.send(JSON.stringify(msg))
}
const app = new Hono const app = new Hono
app.get("/tunnel", c => { app.get("/tunnel", c => {
@ -40,6 +39,7 @@ app.get("/tunnel", c => {
const name = randomName() const name = randomName()
connections[name] = { app, ws } connections[name] = { app, ws }
console.log(`connection opened: ${name} -> ${app}`) console.log(`connection opened: ${name} -> ${app}`)
send(ws, { subdomain: name })
}, },
onClose: (_event, ws) => { onClose: (_event, ws) => {
for (const name of Object.keys(connections)) for (const name of Object.keys(connections))
@ -96,6 +96,12 @@ app.get("*", async c => {
}) })
}) })
function send(connection: any, msg: Request | Success) {
console.log("sending", msg)
connection.send(JSON.stringify(msg))
}
function randomID(): string { function randomID(): string {
return Math.random().toString(36).slice(2, 10) return Math.random().toString(36).slice(2, 10)
} }