refactor: 统一端口配置,通过 IPC 获取而非硬编码

This commit is contained in:
2026-03-13 21:20:31 +08:00
parent 1be470f45b
commit f692961823
4 changed files with 15 additions and 5 deletions

View File

@@ -366,6 +366,10 @@ ipcMain.handle('xc-opencode-web-get-status', () => {
return xcOpenCodeWebService.getStatus(); return xcOpenCodeWebService.getStatus();
}); });
ipcMain.handle('xc-opencode-web-get-port', () => {
return { port: xcOpenCodeWebService.port };
});
ipcMain.handle('xc-opencode-web-start', async () => { ipcMain.handle('xc-opencode-web-start', async () => {
return await xcOpenCodeWebService.start(); return await xcOpenCodeWebService.start();
}); });

View File

@@ -44,4 +44,5 @@ contextBridge.exposeInMainWorld('electronAPI', {
xcOpenCodeWebStart: () => ipcRenderer.invoke('xc-opencode-web-start'), xcOpenCodeWebStart: () => ipcRenderer.invoke('xc-opencode-web-start'),
xcOpenCodeWebStop: () => ipcRenderer.invoke('xc-opencode-web-stop'), xcOpenCodeWebStop: () => ipcRenderer.invoke('xc-opencode-web-stop'),
xcOpenCodeWebGetStatus: () => ipcRenderer.invoke('xc-opencode-web-get-status'), xcOpenCodeWebGetStatus: () => ipcRenderer.invoke('xc-opencode-web-get-status'),
xcOpenCodeWebGetPort: () => ipcRenderer.invoke('xc-opencode-web-get-port'),
}) })

View File

@@ -1,16 +1,20 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
const XCOPENCODEWEB_PORT = 3002
export const OpenCodePage: React.FC = () => { export const OpenCodePage: React.FC = () => {
const [isRunning, setIsRunning] = useState(false) const [isRunning, setIsRunning] = useState(false)
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const [port, setPort] = useState<number>(3002)
useEffect(() => { useEffect(() => {
let mounted = true let mounted = true
const startService = async () => { const init = async () => {
try { try {
const portResult = await window.electronAPI.xcOpenCodeWebGetPort()
if (mounted) {
setPort(portResult.port)
}
const result = await window.electronAPI.xcOpenCodeWebStart() const result = await window.electronAPI.xcOpenCodeWebStart()
if (mounted) { if (mounted) {
if (result.success) { if (result.success) {
@@ -27,7 +31,7 @@ export const OpenCodePage: React.FC = () => {
} }
} }
startService() init()
return () => { return () => {
mounted = false mounted = false
@@ -51,7 +55,7 @@ export const OpenCodePage: React.FC = () => {
</div> </div>
{isRunning && ( {isRunning && (
<iframe <iframe
src={`http://localhost:${XCOPENCODEWEB_PORT}`} src={`http://localhost:${port}`}
className="flex-1 w-full border-0" className="flex-1 w-full border-0"
title="XCOpenCodeWeb" title="XCOpenCodeWeb"
/> />

View File

@@ -43,6 +43,7 @@ export interface ElectronAPI {
xcOpenCodeWebStart: () => Promise<{ success: boolean; error?: string }> xcOpenCodeWebStart: () => Promise<{ success: boolean; error?: string }>
xcOpenCodeWebStop: () => Promise<{ success: boolean; error?: string }> xcOpenCodeWebStop: () => Promise<{ success: boolean; error?: string }>
xcOpenCodeWebGetStatus: () => Promise<{ running: boolean; port: number }> xcOpenCodeWebGetStatus: () => Promise<{ running: boolean; port: number }>
xcOpenCodeWebGetPort: () => Promise<{ port: number }>
} }
declare global { declare global {