fix(stage-tamagotchi): rollback when wss enable failed, show errors

This commit is contained in:
LemonNeko
2026-04-03 18:28:07 +08:00
parent b43a527275
commit 1fccad5fc7
5 changed files with 203 additions and 72 deletions
+24 -32
View File
@@ -112,27 +112,28 @@ export function createServer(opts?: ServerOptions): Server {
}
const secureEnabled = options?.tlsConfig != null
const h3App = setupApp()
const port = options.port
const hostname = options.hostname
const instance = serve(h3App.app, {
// @ts-expect-error - the .crossws property wasn't extended in types
plugins: [ws({ resolve: async req => (await h3App.app.fetch(req)).crossws })],
port,
hostname,
tls: options?.tlsConfig || undefined,
reusePort: true,
silent: true,
manual: true,
gracefulShutdown: {
forceTimeout: 0.5,
gracefulTimeout: 0.5,
},
})
try {
const h3App = setupApp()
const port = options.port
const hostname = options.hostname
const instance = serve(h3App.app, {
// @ts-expect-error - the .crossws property wasn't extended in types
plugins: [ws({ resolve: async req => (await h3App.app.fetch(req)).crossws })],
port,
hostname,
tls: options?.tlsConfig || undefined,
reusePort: true,
silent: true,
manual: true,
gracefulShutdown: {
forceTimeout: 0.5,
gracefulTimeout: 0.5,
},
})
await instance.serve()
serverInstance = {
close: async (closeActiveConnections = false) => {
@@ -144,19 +145,6 @@ export function createServer(opts?: ServerOptions): Server {
},
}
const servePromise = instance.serve()
if (servePromise instanceof Promise) {
servePromise.catch((error) => {
const nodejsError = error as NodeJS.ErrnoException
if ('code' in nodejsError && nodejsError.code === 'EADDRINUSE') {
log.withError(error).warn('Port already in use, assuming server is already running')
return
}
log.withError(error).error('Error serving WebSocket server')
})
}
const protocol = secureEnabled ? 'wss' : 'ws'
if (hostname === '0.0.0.0') {
const ips = getLocalIPs().filter(ip => ip !== '127.0.0.1' && ip !== '::1')
@@ -168,7 +156,11 @@ export function createServer(opts?: ServerOptions): Server {
}
}
catch (error) {
serverInstance = null
h3App.closeAllPeers()
await instance.close(true).catch(() => {})
log.withError(error).error('failed to start WebSocket server')
throw error
}
}