disconnect
This commit is contained in:
parent
a01e45b2b0
commit
0b6ff7f854
|
|
@ -1,13 +1,14 @@
|
|||
import nose from "./server"
|
||||
|
||||
// const SNEAKER_URL = "sneaker-ep2i.onrender.com"
|
||||
// const SNEAKER_TLS = true
|
||||
const SNEAKER_URL = "localhost:3100"
|
||||
const SNEAKER_TLS = false
|
||||
const SNEAKER_URL = "nose.space"
|
||||
const SNEAKER_TLS = true
|
||||
// const SNEAKER_URL = "localhost:3100"
|
||||
// const SNEAKER_TLS = false
|
||||
|
||||
type Connection = {
|
||||
subdomain: string
|
||||
ws: any
|
||||
close: boolean // manual close
|
||||
}
|
||||
const conns: Record<string, Connection> = {}
|
||||
|
||||
|
|
@ -35,6 +36,16 @@ export function sneakers(): string[] {
|
|||
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
|
||||
export async function connectSneaker(app: string, subdomain = ""): Promise<string> {
|
||||
if (conns[app]) {
|
||||
|
|
@ -49,8 +60,9 @@ export async function connectSneaker(app: string, subdomain = ""): Promise<strin
|
|||
let promise = new Promise<string>(res => resolve = res)
|
||||
|
||||
ws.onclose = e => {
|
||||
if (!conns[app]?.close)
|
||||
setTimeout(() => connectSneaker(app, subdomain), 1000) // simple retry
|
||||
delete conns[app]
|
||||
setTimeout(() => connectSneaker(app, subdomain), 1000) // simple retry
|
||||
}
|
||||
|
||||
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())
|
||||
|
||||
if (msg.subdomain) {
|
||||
conns[app] = { subdomain: msg.subdomain, ws }
|
||||
conns[app] = { subdomain: msg.subdomain, ws, close: false }
|
||||
subdomain = msg.subdomain
|
||||
resolve(msg.subdomain)
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user