Skip to content

Commit

Permalink
fix 可用端口获取处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurris committed Jun 22, 2023
1 parent 4f018ad commit b0235f0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
1 change: 0 additions & 1 deletion LiveBackgroundService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
25 changes: 8 additions & 17 deletions danmu-ui/electron/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
}


Expand Down Expand Up @@ -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' })
Expand All @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions danmu-ui/electron/backgroundService.ts
Original file line number Diff line number Diff line change
@@ -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) => {

Expand Down Expand Up @@ -53,3 +63,13 @@ const runBackgroundService = (port: number) => {
}
});
}


const judgePorCanUse = (port: number) => {
const command = `netstat -ano|findstr "${port}"`;
return new Promise<boolean>((resolve) => {
exec(command, (error: any, stdout: string, stderr: string) => {
resolve(stdout === "");
});
})
}
4 changes: 2 additions & 2 deletions danmu-ui/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/',
component: import('../views/Login.vue')
path: "/",
redirect: "/login",
},
{
path: '/login',