baoshiwei
6 天以前 5bf14aed888cd0e258e325c65f14022dad02985b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { invoke } from '@tauri-apps/api/core';
 
export async function startPipeServer(pipeName: string): Promise<void> {
  try {
    await invoke('start_pipe_server_command', { pipeName });
    console.log(`Pipe server started on \\\.\\pipe\\${pipeName}`);
  } catch (err) {
    console.error('Failed to start pipe server:', err);
  }
}
 
export async function sendToPipe(pipeName: string, message: string): Promise<string> {
  // 添加重试机制
  const maxRetries = 3;
  let retryCount = 0;
  let lastError: any;
  
  while (retryCount < maxRetries) {
    try {
      return await invoke('send_pipe_message', { pipeName, message });
    } catch (err) {
      lastError = err;
      console.warn(`管道通信失败,正在重试 (${retryCount + 1}/${maxRetries}):`, err);
      retryCount++;
      
      // 等待一段时间再重试
      if (retryCount < maxRetries) {
        await new Promise(resolve => setTimeout(resolve, 500 * retryCount));
      }
    }
  }
  
  // 所有重试都失败,抛出最后一个错误
  throw lastError;
}
 
export async function getLatestWsData(): Promise<string | null> {
  try {
    const result = await invoke<string | null>('get_latest_ws_data');
    console.log("getlatestwsData", result);
    return result;
  } catch (err) {
    console.error('获取 WebSocket 数据失败:', err);
    return null;
  }
}