Skip to content

Commit 5995e9c

Browse files
committed
Update BackendStatus.tsx
1 parent c10ff2f commit 5995e9c

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/games/demo-with-backend/components/BackendStatus.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2+
import { trpc } from '../services/trpc';
23

34
export const BackendStatus: React.FC = () => {
45
const [status, setStatus] = useState<'checking' | 'connected' | 'error'>('checking');
@@ -7,24 +8,14 @@ export const BackendStatus: React.FC = () => {
78
useEffect(() => {
89
const checkBackend = async () => {
910
try {
10-
const response = await fetch('http://localhost:3000/api/trpc/auth.healthCheck', {
11-
method: 'GET',
12-
headers: {
13-
'Content-Type': 'application/json',
14-
}
15-
});
16-
17-
if (response.ok) {
18-
const data = await response.json();
19-
if (data.result?.data?.status === 'ok') {
20-
setStatus('connected');
21-
} else {
22-
setStatus('error');
23-
setError('后端服务异常');
24-
}
11+
// 使用 tRPC 的 healthCheck 方法来检查后端连接
12+
const result = await trpc.auth.healthCheck.query();
13+
14+
if (result.status === 'ok') {
15+
setStatus('connected');
2516
} else {
2617
setStatus('error');
27-
setError(`HTTP ${response.status}: ${response.statusText}`);
18+
setError('后端服务异常: ' + (result.message || '未知错误'));
2819
}
2920
} catch (err) {
3021
setStatus('error');
@@ -54,7 +45,7 @@ export const BackendStatus: React.FC = () => {
5445
<p>请确保后端服务正在运行:</p>
5546
<p>1. 启动 Basic-Web-Game-Backend 项目</p>
5647
<p>2. 运行 npm run dev</p>
57-
<p>3. 确保服务运行在 http://localhost:3000</p>
48+
<p>3. 确保服务运行在 ${import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'}</p>
5849
</div>
5950
)}
6051
</div>

0 commit comments

Comments
 (0)