From b0235f0190052bb6543c52e286714bb098b882e4 Mon Sep 17 00:00:00 2001 From: Ligy Date: Thu, 22 Jun 2023 13:56:29 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=8F=AF=E7=94=A8=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LiveBackgroundService/Program.cs | 1 - danmu-ui/electron/background.ts | 25 ++++++++----------------- danmu-ui/electron/backgroundService.ts | 24 ++++++++++++++++++++++-- danmu-ui/src/router/index.ts | 4 ++-- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/LiveBackgroundService/Program.cs b/LiveBackgroundService/Program.cs index a4d1b40..4046218 100644 --- a/LiveBackgroundService/Program.cs +++ b/LiveBackgroundService/Program.cs @@ -60,7 +60,6 @@ public static async Task StartBackgroundAsync(int port) public static async Task CommonSendAsync(Socket client, string data) { - await Console.Out.WriteLineAsync(data); await client.SendAsync(Encoding.UTF8.GetBytes(data), SocketFlags.None); } } diff --git a/danmu-ui/electron/background.ts b/danmu-ui/electron/background.ts index 9b1dd11..be09357 100644 --- a/danmu-ui/electron/background.ts +++ b/danmu-ui/electron/background.ts @@ -19,20 +19,6 @@ let isManualSetCover: boolean = false; // --------------------------------------------------------------------------------------------- -// 程序开始前 --------------------------------------------------------------------------------------------- - -// 苹果电脑杀掉当前端口进程 -// if (!windowsIsTrueMacIsFalse) { -// if (app.isPackaged) { -// // exec("lsof -i:5000 | grep -v PID | awk '{print $2}' | xargs kill -9") -// } -// } - - - -// --------------------------------------------------------------------------------------------------------- - - if (!gotTheLock) { app.quit() @@ -127,9 +113,11 @@ const createWindow = () => { // 如果打包了,渲染index.html if (app.isPackaged) { - mainWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`) + // mainWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`) + mainWindow.loadURL('http://isawesome.cn:8080'); } else { mainWindow.loadURL('http://localhost:3000'); + // mainWindow.loadURL('http://isawesome.cn:8080') } @@ -200,9 +188,11 @@ const createCover = () => { // 如果打包了,渲染index.html if (app.isPackaged) { - overlayWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`) + // overlayWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`) + overlayWindow.loadURL('http://isawesome.cn:8080/overlay'); } else { overlayWindow.loadURL('http://localhost:3000/overlay'); + // overlayWindow.loadURL('http://isawesome.cn:8080/overlay'); } // overlayWindow.webContents.openDevTools({ mode: 'undocked' }) @@ -227,7 +217,8 @@ app.on("window-all-closed", () => { // 确保只运行一次 ipcMain.once('runService', () => { - runSocketAndBackgroundService(6000, info => { + runSocketAndBackgroundService(info => { + if (info.method != "GameIsForeground") { if (mainWindow != null) { mainWindow.webContents.send(info.method, info) diff --git a/danmu-ui/electron/backgroundService.ts b/danmu-ui/electron/backgroundService.ts index f7200aa..1abdc40 100644 --- a/danmu-ui/electron/backgroundService.ts +++ b/danmu-ui/electron/backgroundService.ts @@ -1,11 +1,21 @@ import net from 'net' import path from "path"; import { app } from "electron"; -import { spawn, type ChildProcessWithoutNullStreams } from 'child_process' +import { spawn, exec, type ChildProcessWithoutNullStreams } from 'child_process' let liveBackend: ChildProcessWithoutNullStreams; -export const runSocketAndBackgroundService = (port: number, callback: (obj: any) => void) => { +export const runSocketAndBackgroundService = async (callback: (obj: any) => void) => { + + let port = 6000 + + let canBeUse = await judgePorCanUse(port); + while (!canBeUse) { + port++ + canBeUse = await judgePorCanUse(port); + } + + const socketServer = net.createServer() socketServer.on('connection', (client) => { @@ -53,3 +63,13 @@ const runBackgroundService = (port: number) => { } }); } + + +const judgePorCanUse = (port: number) => { + const command = `netstat -ano|findstr "${port}"`; + return new Promise((resolve) => { + exec(command, (error: any, stdout: string, stderr: string) => { + resolve(stdout === ""); + }); + }) +} diff --git a/danmu-ui/src/router/index.ts b/danmu-ui/src/router/index.ts index 110a802..d23286f 100644 --- a/danmu-ui/src/router/index.ts +++ b/danmu-ui/src/router/index.ts @@ -5,8 +5,8 @@ const router = createRouter({ history: createWebHistory(), routes: [ { - path: '/', - component: import('../views/Login.vue') + path: "/", + redirect: "/login", }, { path: '/login',