From 5afec03548bc74971bbe28a6e2a9467140644856 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 30 Nov 2023 23:39:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E5=88=87?= =?UTF-8?q?=E6=8D=A2=20node=20=E5=92=8C=20linux=20=E6=BA=90=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/data/sock.ts | 4 +++- back/services/system.ts | 24 ++++++++++++------- src/locales/en-US.json | 2 +- src/locales/zh-CN.json | 2 +- src/pages/setting/dependence.tsx | 40 +++++++++++++++++++++----------- src/utils/type.ts | 4 +++- 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/back/data/sock.ts b/back/data/sock.ts index 12dca74d..8dae8e13 100644 --- a/back/data/sock.ts +++ b/back/data/sock.ts @@ -17,4 +17,6 @@ export type SockMessageType = | 'updateSystemVersion' | 'manuallyRunScript' | 'runSubscriptionEnd' - | 'reloadSystem'; + | 'reloadSystem' + | 'updateNodeMirror' + | 'updateLinuxMirror'; diff --git a/back/services/system.ts b/back/services/system.ts index 9441e32e..4dc2c8a9 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -142,15 +142,19 @@ export default class SystemService { { onStart: async (cp) => { res.setHeader('QL-Task-Pid', `${cp.pid}`); - }, - onEnd: async () => { res.end(); }, + onEnd: async () => { + this.sockService.sendMessage({ + type: 'updateNodeMirror', + message: 'update node mirror end', + }); + }, onError: async (message: string) => { - res.write(`\n${message}`); + this.sockService.sendMessage({ type: 'updateNodeMirror', message }); }, onLog: async (message: string) => { - res.write(`\n${message}`); + this.sockService.sendMessage({ type: 'updateNodeMirror', message }); }, }, { @@ -192,15 +196,19 @@ export default class SystemService { { onStart: async (cp) => { res.setHeader('QL-Task-Pid', `${cp.pid}`); - }, - onEnd: async () => { res.end(); }, + onEnd: async () => { + this.sockService.sendMessage({ + type: 'updateLinuxMirror', + message: 'update linux mirror end', + }); + }, onError: async (message: string) => { - res.write(`\n${message}`); + this.sockService.sendMessage({ type: 'updateLinuxMirror', message }); }, onLog: async (message: string) => { - res.write(`\n${message}`); + this.sockService.sendMessage({ type: 'updateLinuxMirror', message }); }, }, { diff --git a/src/locales/en-US.json b/src/locales/en-US.json index e3f627ee..36df48e8 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -475,5 +475,5 @@ "代理地址, 支持HTTP(S)/SOCK5": "Proxy Address, supports HTTP(S)/SOCK5", "NPM 镜像源": "NPM Mirror Source", "PyPI 镜像源": "PyPI Mirror Source", - "alpine linux 镜像源": "Alpine Linux Mirror Source" + "alpine linux 镜像源, 例如 mirrors.aliyun.com": "Alpine Linux Mirror Source, e.g. mirrors.aliyun.com" } diff --git a/src/locales/zh-CN.json b/src/locales/zh-CN.json index 53f31bcc..ae7d76fb 100644 --- a/src/locales/zh-CN.json +++ b/src/locales/zh-CN.json @@ -475,5 +475,5 @@ "代理地址, 支持HTTP(S)/SOCK5": "代理地址, 支持HTTP(S)/SOCK5", "NPM 镜像源": "NPM 镜像源", "PyPI 镜像源": "PyPI 镜像源", - "alpine linux 镜像源": "alpine linux 镜像源" + "alpine linux 镜像源, 例如 mirrors.aliyun.com": "alpine linux 镜像源, 例如 mirrors.aliyun.com" } diff --git a/src/pages/setting/dependence.tsx b/src/pages/setting/dependence.tsx index 241fce3d..d5510210 100644 --- a/src/pages/setting/dependence.tsx +++ b/src/pages/setting/dependence.tsx @@ -6,6 +6,7 @@ import { request } from '@/utils/http'; import './index.less'; import Ansi from 'ansi-to-react'; import pick from 'lodash/pick'; +import WebSocketManager from '@/utils/websocket'; const dataMap = { 'dependence-proxy': 'dependenceProxy', @@ -38,18 +39,13 @@ const Dependence = () => { }; const updateSystemConfigStream = (path: keyof typeof dataMap) => { - setLog('执行中...'); + setLog('执行中...\n'); request .put( `${config.apiPrefix}system/config/${path}`, pick(systemConfig, dataMap[path]), - { - responseType: 'stream', - }, ) - .then((res) => { - setLog(() => res); - }) + .then((res) => {}) .catch((error: any) => { console.log(error); }); @@ -72,6 +68,22 @@ const Dependence = () => { }); }; + const handleMessage = (payload: any) => { + const { message } = payload; + setLog((p) => `${p}${message}`); + }; + + useEffect(() => { + const ws = WebSocketManager.getInstance(); + ws.subscribe('updateNodeMirror', handleMessage); + ws.subscribe('updateLinuxMirror', handleMessage); + + return () => { + ws.subscribe('updateNodeMirror', handleMessage); + ws.unsubscribe('updateLinuxMirror', handleMessage); + }; + }, []); + useEffect(() => { getSystemConfig(); }, []); @@ -87,7 +99,7 @@ const Dependence = () => { { setSystemConfig({ @@ -110,7 +122,7 @@ const Dependence = () => { { @@ -134,7 +146,7 @@ const Dependence = () => { { @@ -158,8 +170,10 @@ const Dependence = () => { { setSystemConfig({ @@ -185,7 +199,7 @@ const Dependence = () => { fontFamily: 'Source Code Pro', zoom: 0.83, maxHeight: '100%', - overflowY: 'auto' + overflowY: 'auto', }} > {log} diff --git a/src/utils/type.ts b/src/utils/type.ts index 9ce55269..ab13e686 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -5,4 +5,6 @@ export type SockMessageType = | 'updateSystemVersion' | 'manuallyRunScript' | 'runSubscriptionEnd' - | 'reloadSystem'; \ No newline at end of file + | 'reloadSystem' + | 'updateNodeMirror' + | 'updateLinuxMirror';