fix closing

This commit is contained in:
Chris Wanstrath 2025-09-23 21:43:29 -07:00
parent 80559999ab
commit dcf104a77d

View File

@ -37,20 +37,17 @@ app.get("/tunnel", c => {
if (subdomain && connections[subdomain]) if (subdomain && connections[subdomain])
return c.text("subdomain taken", 502) return c.text("subdomain taken", 502)
const name = subdomain || randomName()
return upgradeWebSocket(c, { return upgradeWebSocket(c, {
async onOpen(_event, ws) { async onOpen(_event, ws) {
const name = subdomain || 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 }) send(ws, { subdomain: name })
}, },
onClose: (_event, ws) => { onClose: (_event, ws) => {
for (const name of Object.keys(connections)) console.log("connection closed:", name)
if (connections[name]?.ws === ws) { delete connections[name]
console.log("connection closed:", name)
delete connections[name]
break
}
}, },
async onMessage(event, _ws) { async onMessage(event, _ws) {
const msg = JSON.parse(event.data.toString()) const msg = JSON.parse(event.data.toString())