disconnect

This commit is contained in:
Chris Wanstrath 2025-09-23 22:00:15 -07:00
parent a01e45b2b0
commit 0b6ff7f854

View File

@ -1,13 +1,14 @@
import nose from "./server" import nose from "./server"
// const SNEAKER_URL = "sneaker-ep2i.onrender.com" const SNEAKER_URL = "nose.space"
// const SNEAKER_TLS = true const SNEAKER_TLS = true
const SNEAKER_URL = "localhost:3100" // const SNEAKER_URL = "localhost:3100"
const SNEAKER_TLS = false // const SNEAKER_TLS = false
type Connection = { type Connection = {
subdomain: string subdomain: string
ws: any ws: any
close: boolean // manual close
} }
const conns: Record<string, Connection> = {} const conns: Record<string, Connection> = {}
@ -35,6 +36,16 @@ export function sneakers(): string[] {
return Object.keys(conns) return Object.keys(conns)
} }
export function disconnectSneaker(app: string): boolean {
if (!sneakers().includes(app) || !conns[app])
return false
conns[app].close = true
conns[app].ws.close()
return true
}
// returns the sneaker subdomain if successful // returns the sneaker subdomain if successful
export async function connectSneaker(app: string, subdomain = ""): Promise<string> { export async function connectSneaker(app: string, subdomain = ""): Promise<string> {
if (conns[app]) { if (conns[app]) {
@ -49,8 +60,9 @@ export async function connectSneaker(app: string, subdomain = ""): Promise<strin
let promise = new Promise<string>(res => resolve = res) let promise = new Promise<string>(res => resolve = res)
ws.onclose = e => { ws.onclose = e => {
delete conns[app] if (!conns[app]?.close)
setTimeout(() => connectSneaker(app, subdomain), 1000) // simple retry setTimeout(() => connectSneaker(app, subdomain), 1000) // simple retry
delete conns[app]
} }
ws.onerror = e => console.error("sneaker error", e) ws.onerror = e => console.error("sneaker error", e)
@ -59,7 +71,7 @@ export async function connectSneaker(app: string, subdomain = ""): Promise<strin
const msg = JSON.parse(event.data.toString()) const msg = JSON.parse(event.data.toString())
if (msg.subdomain) { if (msg.subdomain) {
conns[app] = { subdomain: msg.subdomain, ws } conns[app] = { subdomain: msg.subdomain, ws, close: false }
subdomain = msg.subdomain subdomain = msg.subdomain
resolve(msg.subdomain) resolve(msg.subdomain)
return return