A:增加实时接口访问
This commit is contained in:
1 parent
77757bdadc
commit
4d60f349df
3 files changed
+61
-36
No files matched your search
@@ -9,6 +9,7 @@
|
||||
* c'est de voir le monde tel qu'il est et de l'aimer.
|
||||
*/
|
||||
import type { HttpConfig } from '@/types/http.ts'
|
||||
import { httpClient } from '@/utils/http'
|
||||
|
||||
// 2. 配置管理器 (config/httpConfig.ts)
|
||||
class HttpConfigManager {
|
||||
@@ -26,7 +27,10 @@ class HttpConfigManager {
|
||||
setConfig(config: Partial<HttpConfig>): void {
|
||||
this.config = { ...this.config, ...config };
|
||||
}
|
||||
|
||||
setUrl(baseUrl:string):void{
|
||||
this.config.baseURL = baseUrl;
|
||||
httpClient.updateConfig(this.config);
|
||||
}
|
||||
/**
|
||||
* 获取配置
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,35 @@
|
||||
import CSharpBridgeV2 from '@/correspond/CSharpBridgeV2'
|
||||
import websocketPlugin from '@/plugins/websocketPlugin'
|
||||
import { useWebSocketStore } from '@/stores/websocketStore'
|
||||
import { httpConfig } from '@/config/httpConfig'
|
||||
|
||||
// 注册 C# 函数
|
||||
window.externalFunctions = {}
|
||||
|
||||
// 监听WebSocket地址
|
||||
const bridge = CSharpBridgeV2?.getBridge()
|
||||
if (bridge) {
|
||||
const intervalId = setInterval(() => {
|
||||
if (webSocketStore.isInitialized) {
|
||||
clearInterval(intervalId)
|
||||
return
|
||||
}
|
||||
if (bridge) {
|
||||
bridge.send('GetWebSocketPath', {}, (data) => {
|
||||
webSocketStore.initialize(data)
|
||||
console.log(data, '======')
|
||||
})
|
||||
bridge.send('GetHttpPath', {}, (data) => {
|
||||
httpConfig.setUrl(data + '/api')
|
||||
console.log(data, 'HTTP 路径')
|
||||
})
|
||||
}
|
||||
}, 500)
|
||||
} else {
|
||||
// todo: 2025/6/25 08:32 开发阶段直接使用固定节点
|
||||
webSocketStore.initialize('ws://127.0.0.1:8267')
|
||||
}
|
||||
|
||||
import './assets/main.css'
|
||||
import { createApp } from 'vue'
|
||||
import { createPinia } from 'pinia'
|
||||
@@ -5,14 +37,10 @@ import { createPinia } from 'pinia'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
|
||||
import CSharpBridgeV2 from '@/correspond/CSharpBridgeV2'
|
||||
import websocketPlugin from '@/plugins/websocketPlugin'
|
||||
import { useWebSocketStore } from '@/stores/websocketStore'
|
||||
import { createNaiveUI } from './naive'
|
||||
import { useThemeStore } from '@/stores/theme'
|
||||
import { useUuidStore } from '@/stores/uuidStore'
|
||||
import { logB } from '@/utils/logHelper/logUtils'
|
||||
import { httpConfig } from '@/config/httpConfig'
|
||||
|
||||
// Vue Virtual Scroller
|
||||
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
|
||||
@@ -48,28 +76,5 @@ app.use(httpPlugin)
|
||||
|
||||
app.mount('#app')
|
||||
|
||||
// 注册 C# 函数
|
||||
window.externalFunctions = {}
|
||||
|
||||
// 监听获取 WebSocket 链接
|
||||
const webSocketStore = useWebSocketStore()
|
||||
|
||||
// 监听WebSocket地址
|
||||
const bridge = CSharpBridgeV2?.getBridge()
|
||||
if (bridge) {
|
||||
const intervalId = setInterval(() => {
|
||||
if (webSocketStore.isInitialized) {
|
||||
clearInterval(intervalId)
|
||||
return
|
||||
}
|
||||
if (bridge) {
|
||||
bridge.send('GetWebSocketPath', {}, (data) => {
|
||||
webSocketStore.initialize(data)
|
||||
console.log(data, '======')
|
||||
})
|
||||
}
|
||||
}, 500)
|
||||
} else {
|
||||
// todo: 2025/6/25 08:32 开发阶段直接使用固定节点
|
||||
webSocketStore.initialize('ws://127.0.0.1:8267')
|
||||
}
|
||||
@@ -202,6 +202,29 @@ const setAppItemRef = (el: unknown, index: number) => {
|
||||
appItemRefs.value[index] = el
|
||||
}
|
||||
}
|
||||
function postWithPolling(url: string, data: any, interval = 2000, maxRetries = 10) {
|
||||
let attempt = 0;
|
||||
|
||||
const tryRequest = () => {
|
||||
httpClient
|
||||
.post(url, JSON.stringify(data))
|
||||
.then((res: ResponseModel) => {
|
||||
console.log('订阅应用信息成功:', res);
|
||||
// 成功后就停止
|
||||
})
|
||||
.catch((err) => {
|
||||
attempt++;
|
||||
if (attempt < maxRetries) {
|
||||
console.warn(`请求失败(${attempt}/${maxRetries}),${interval}ms 后重试...`);
|
||||
setTimeout(tryRequest, interval);
|
||||
} else {
|
||||
console.error(`请求最终失败,已达到最大重试次数:`, err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
tryRequest();
|
||||
}
|
||||
|
||||
// 监听 WebSocket 连接状态
|
||||
onMounted(() => {
|
||||
@@ -216,14 +239,7 @@ onMounted(() => {
|
||||
interval: 1000,
|
||||
}
|
||||
|
||||
httpClient
|
||||
.post(`/subscribe-application`, JSON.stringify(subAppInfo))
|
||||
.then((res: ResponseModel) => {
|
||||
console.log('订阅应用信息成功:', res)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('订阅应用信息失败:', err)
|
||||
})
|
||||
postWithPolling("/subscribe-application", subAppInfo, 500, 20);
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
|
||||
Reference in new issue
Block a user