Skip to content

Commit 6526fd1

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

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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

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

43
export const BackendStatus: React.FC = () => {
54
const [status, setStatus] = useState<'checking' | 'connected' | 'error'>('checking');
@@ -8,14 +7,26 @@ export const BackendStatus: React.FC = () => {
87
useEffect(() => {
98
const checkBackend = async () => {
109
try {
11-
// 使用 tRPC 的 healthCheck 方法来检查后端连接
12-
const result = await trpc.auth.healthCheck.query();
13-
14-
if (result.status === 'ok') {
15-
setStatus('connected');
10+
// 使用环境变量中的后端URL,如果没有则回退到localhost
11+
const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000';
12+
const response = await fetch(`${backendUrl}/api/trpc/auth.healthCheck`, {
13+
method: 'GET',
14+
headers: {
15+
'Content-Type': 'application/json',
16+
}
17+
});
18+
19+
if (response.ok) {
20+
const data = await response.json();
21+
if (data.result?.data?.status === 'ok') {
22+
setStatus('connected');
23+
} else {
24+
setStatus('error');
25+
setError('后端服务异常');
26+
}
1627
} else {
1728
setStatus('error');
18-
setError('后端服务异常: ' + (result.message || '未知错误'));
29+
setError(`HTTP ${response.status}: ${response.statusText}`);
1930
}
2031
} catch (err) {
2132
setStatus('error');
@@ -45,7 +56,7 @@ export const BackendStatus: React.FC = () => {
4556
<p>请确保后端服务正在运行:</p>
4657
<p>1. 启动 Basic-Web-Game-Backend 项目</p>
4758
<p>2. 运行 npm run dev</p>
48-
<p>3. 确保服务运行在 ${import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'}</p>
59+
<p>3. 确保服务运行在 {import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'}</p>
4960
</div>
5061
)}
5162
</div>

0 commit comments

Comments
 (0)