Skip to content

Commit

Permalink
✨ feat: 自动切换可用端口
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Jun 4, 2024
1 parent ad2422d commit b146dc0
Show file tree
Hide file tree
Showing 5 changed files with 931 additions and 884 deletions.
19 changes: 8 additions & 11 deletions electron.vite.config.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { resolve } from "path";
import {
defineConfig,
externalizeDepsPlugin,
loadEnv,
splitVendorChunkPlugin,
} from "electron-vite";
import { defineConfig, externalizeDepsPlugin, loadEnv } from "electron-vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
import { VitePWA } from "vite-plugin-pwa";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import viteCompression from "vite-plugin-compression";
import checkPort from "./electron/main/utils/checkPort";

export default defineConfig(({ mode }) => {
export default defineConfig(async ({ mode }) => {
// 读取环境变量
const getEnv = (name) => {
return loadEnv(mode, process.cwd())[name];
};
// 获取端口
const devPort = await checkPort(getEnv("MAIN_VITE_DEV_PORT"));
const serverPort = await checkPort(getEnv("MAIN_VITE_SERVER_PORT"));
// 返回配置
return {
// 主进程
Expand Down Expand Up @@ -70,8 +69,6 @@ export default defineConfig(({ mode }) => {
}),
// viteCompression
viteCompression(),
// splitVendorChunkPlugin
splitVendorChunkPlugin(),
// PWA
VitePWA({
registerType: "autoUpdate",
Expand Down Expand Up @@ -131,11 +128,11 @@ export default defineConfig(({ mode }) => {
],
// 服务器配置
server: {
port: getEnv("MAIN_VITE_DEV_PORT"),
port: devPort,
// 代理
proxy: {
"/api": {
target: `http://${getEnv("MAIN_VITE_SERVER_HOST")}:${getEnv("MAIN_VITE_SERVER_PORT")}`,
target: `http://${getEnv("MAIN_VITE_SERVER_HOST")}:${serverPort}`,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
Expand Down
4 changes: 3 additions & 1 deletion electron/main/startNcmServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import netEaseApi from "NeteaseCloudMusicApi";
import checkPort from "@main/utils/checkPort";

/**
* 启动网易云音乐 API 服务器
Expand All @@ -15,6 +16,7 @@ export const startNcmServer = async (
host: "127.0.0.1",
},
) => {
console.log(options);
const serverPort = await checkPort(options.port);
options.port = serverPort;
return await netEaseApi.serveNcmApi(options);
};
37 changes: 37 additions & 0 deletions electron/main/utils/checkPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import net from "net";

/**
* 检查端口是否可用, 如果被占用或不可访问,则尝试下一个端口
* @param {number} port 端口号
* @param {number} [maxPort=65535] 端口号上限
* @returns {Promise<number>} 返回可用的端口号
*/
const checkPort = (port, maxPort = 65535) => {
return new Promise((resolve, reject) => {
if (port > maxPort) {
reject(new Error(`${port} 超出端口范围,无法找到可用端口`));
return;
}

port = Number(port);

const server = net.createServer();

server.listen(port, "0.0.0.0", () => {
server.once("close", () => {
resolve(port);
});
server.close();
});

server.on("error", (err) => {
if (err.code === "EADDRINUSE" || err.code === "EACCES") {
resolve(checkPort(port + 1, maxPort));
} else {
reject(err);
}
});
});
};

export default checkPort;
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "splayer",
"version": "2.0.8",
"version": "2.0.9",
"description": "A minimalist music player",
"main": "./out/main/index.js",
"author": "imsyy",
Expand Down Expand Up @@ -30,7 +30,7 @@
"@electron-toolkit/preload": "^3.0.1",
"@electron-toolkit/utils": "^3.0.0",
"@material/material-color-utilities": "^0.2.7",
"NeteaseCloudMusicApi": "^4.19.8",
"NeteaseCloudMusicApi": "^4.19.9",
"axios": "^1.7.2",
"colorthief": "^2.4.0",
"electron-dl": "^3.5.2",
Expand All @@ -54,22 +54,22 @@
"devDependencies": {
"@electron-toolkit/eslint-config": "^1.0.2",
"@rushstack/eslint-patch": "^1.10.3",
"@vitejs/plugin-vue": "^5.0.4",
"@vitejs/plugin-vue": "^5.0.5",
"@vue/eslint-config-prettier": "^9.0.0",
"ajv": "^8.13.0",
"electron": "^28.3.2",
"ajv": "^8.15.0",
"electron": "^28.3.3",
"electron-builder": "^24.13.3",
"electron-log": "^5.1.4",
"electron-log": "^5.1.5",
"electron-vite": "^2.2.0",
"eslint": "^8.57.0",
"eslint-plugin-vue": "^9.26.0",
"naive-ui": "^2.38.2",
"prettier": "^3.2.5",
"sass": "^1.77.2",
"prettier": "^3.3.0",
"sass": "^1.77.4",
"terser": "^5.31.0",
"unplugin-auto-import": "^0.17.6",
"unplugin-vue-components": "^0.26.0",
"vite": "^5.2.11",
"vite": "^5.2.12",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-pwa": "^0.17.5",
"vue": "3.4.8"
Expand Down
Loading

0 comments on commit b146dc0

Please sign in to comment.