返回提交历史
Modified
src/index.ts
+14
-2
Modified
src/services/configWatcherService.ts
+3
-5
Modified
src/services/webService.ts
+42
-23
XFEstudio/XFESpaceNinjaServer
chore: provide a more helpful error message when port binding fails (#4011)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/4011 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
3610198f
代码差异
3 个文件
+59
-30
@@ -21,7 +21,7 @@ import mongoose from "mongoose";
21
21
import path from "path";
22
22
import child_process from "child_process";
23
23
import { JSONStringify } from "json-with-bigint";
24
import { startWebServer } from "./services/webService.ts";
24
import { startWebServer, type IListenError } from "./services/webService.ts";
25
25
import { syncConfigWithDatabase, validateConfig } from "./services/configWatcherService.ts";
26
26
import { updateWorldStateCollections } from "./services/worldStateService.ts";
27
27
import { repoDir } from "./helpers/pathHelper.ts";
@@ -60,7 +60,19 @@ mongoose
60
60
logger.info("Connected to MongoDB");
61
61
syncConfigWithDatabase();
62
62
63
startWebServer();
63
startWebServer().catch((err: IListenError) => {
64
if (err.port) {
65
logger.error(`Failed to bind port ${err.port}`);
66
if (process.platform == "win32") {
67
logger.error(
68
`You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${err.port}).OwningProcess`
69
);
70
}
71
} else {
72
logger.error(err.message);
73
}
74
process.exit(1);
75
});
64
76
65
77
for (const [what, key] of [
66
78
["IRC", "ircExecutable"],
@@ -65,11 +65,9 @@ chokidar.watch(configPath).on("change", () => {
65
65
sendWsBroadcast({ ports: { http: config.httpPort, https: config.httpsPort } });
66
66
67
67
void stopWebServer().then(() => {
68
try {
69
startWebServer();
70
} catch (e) {
71
logger.error(`Could not start up web server again: ${(e as Error).message}`);
72
}
68
startWebServer().catch((e: Error) => {
69
logger.error(`Could not start up web server again: ${e.message}`);
70
});
73
71
});
74
72
} else {
75
73
sendWsBroadcastToWebui({ config_reloaded: true });
@@ -10,7 +10,12 @@ import { startWsServer, startWssServer, stopWsServers } from "./wsService.ts";
10
10
let httpServer: http.Server | undefined;
11
11
let httpsServer: https.Server | undefined;
12
12
13
export const startWebServer = (): void => {
13
export interface IListenError extends Error {
14
code: string;
15
port?: number;
16
}
17
18
export const startWebServer = (): Promise<void> => {
14
19
const params = getWebServerParams();
15
20
16
21
const tlsOptions = {
@@ -18,34 +23,48 @@ export const startWebServer = (): void => {
18
23
key: fs.readFileSync(params.keyFile)
19
24
};
20
25
21
httpServer = http.createServer(app);
22
httpServer.listen(params.httpPort, params.address, () => {
23
startWsServer(httpServer!);
24
25
logger.info(`HTTP server started on ${params.address}:${params.httpPort}`);
26
27
httpsServer = https.createServer(tlsOptions, app);
28
httpsServer.listen(params.httpsPort, params.address, () => {
29
startWssServer(httpsServer!);
26
return new Promise<void>((resolve, reject) => {
27
httpServer = http.createServer(app);
28
httpServer.on("error", (err: IListenError) => {
29
if (err.code == "EADDRINUSE") {
30
err.port = params.httpPort;
31
}
32
reject(err);
33
});
34
httpServer.listen(params.httpPort, params.address, () => {
35
httpsServer = https.createServer(tlsOptions, app);
36
httpsServer.on("error", (err: IListenError) => {
37
if (err.code == "EADDRINUSE") {
38
err.port = params.httpsPort;
39
}
40
httpServer!.close();
41
reject(err);
42
});
43
httpsServer.listen(params.httpsPort, params.address, () => {
44
startWsServer(httpServer!);
45
startWssServer(httpsServer!);
30
46
31
logger.info(`HTTPS server started on ${params.address}:${params.httpsPort}`);
47
logger.info(`HTTP server started on ${params.address}:${params.httpPort}`);
48
logger.info(`HTTPS server started on ${params.address}:${params.httpsPort}`);
49
logger.info(
50
"Access the WebUI in your browser at http://localhost" +
51
(params.httpPort == 80 ? "" : ":" + params.httpPort)
52
);
32
53
33
logger.info(
34
"Access the WebUI in your browser at http://localhost" +
35
(params.httpPort == 80 ? "" : ":" + params.httpPort)
36
);
54
resolve();
37
55
38
void runWsSelfTest("wss", params.httpsPort).then(ok => {
39
if (!ok) {
40
logger.warn(
41
`WSS self-test failed. The server may not be reachable locally on port ${params.httpsPort}.`
42
);
43
if (process.platform == "win32") {
56
void runWsSelfTest("wss", params.httpsPort).then(ok => {
57
if (!ok) {
44
58
logger.warn(
45
`You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${params.httpsPort}).OwningProcess`
59
`WSS self-test failed. The server may not be reachable locally on port ${params.httpsPort}.`
46
60
);
61
if (process.platform == "win32") {
62
logger.warn(
63
`You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${params.httpsPort}).OwningProcess`
64
);
65
}
47
66
}
48
}
67
});
49
68
});
50
69
});
51
70
});