diff --git a/SimpleDbQuery.cs b/SimpleDbQuery.cs new file mode 100644 index 0000000000000000000000000000000000000000..5fdc3452567f8e7c140b6e390d18616d7bc3fbaa --- /dev/null +++ b/SimpleDbQuery.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using Npgsql; + +namespace SimpleDbQuery +{ + class Program + { + static void Main(string[] args) + { + try + { + // 连接字符串 + string connectionString = "Host=39.105.7.196;Port=5432;Database=TerritoryGame;Username=postgres;Password=xyq0716"; + + // 创建Npgsql连接 + using (var connection = new NpgsqlConnection(connectionString)) + { + connection.Open(); + Console.WriteLine("数据库连接成功"); + + // 创建查询命令 + using (var command = new NpgsqlCommand("SELECT * FROM \"GameRooms\" WHERE \"Status\" = 0", connection)) + { + // 执行查询 + using (var reader = command.ExecuteReader()) + { + Console.WriteLine("查询结果:"); + Console.WriteLine("房间ID | 名称 | 创建时间"); + Console.WriteLine("------------------------"); + + // 读取结果 + while (reader.Read()) + { + Guid id = reader.GetGuid(0); + string name = reader.GetString(1); + DateTime createdAt = reader.GetDateTime(5); + + Console.WriteLine($"{id} | {name} | {createdAt}"); + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"发生错误: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/SimpleDbQuery.csproj b/SimpleDbQuery.csproj new file mode 100644 index 0000000000000000000000000000000000000000..44fac84807d13635d0b930e3e3d8ed579399964d --- /dev/null +++ b/SimpleDbQuery.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + \ No newline at end of file diff --git a/backend/src/TerritoryGame.Api/Hubs/GameHub.cs b/backend/src/TerritoryGame.Api/Hubs/GameHub.cs index 60ff4699bc0297ca10fc418986f324ab5a4de885..1f8b6572232a10c34da9f1ff6a21288eebf412ce 100644 --- a/backend/src/TerritoryGame.Api/Hubs/GameHub.cs +++ b/backend/src/TerritoryGame.Api/Hubs/GameHub.cs @@ -19,8 +19,7 @@ public class GameHub : Hub try { var rooms = await _gameService.GetAvailableRoomsAsync(); - // 直接发送房间数组,而不是包装在对象中 - await Clients.Caller.SendAsync("RoomListUpdated", rooms); + await Clients.Caller.SendAsync("RoomListUpdated", new { rooms }); } catch (Exception ex) { diff --git a/backend/src/TerritoryGame.Application/Services/GameService.cs b/backend/src/TerritoryGame.Application/Services/GameService.cs index 7884ad50ff20652167a6155e394b1ffa89a4880f..dfb18c3932ba4d9006d4459a63a041951b42506d 100644 --- a/backend/src/TerritoryGame.Application/Services/GameService.cs +++ b/backend/src/TerritoryGame.Application/Services/GameService.cs @@ -94,21 +94,33 @@ public class GameService : DomainServices.IGameService { try { - // 调试日志:记录方法开始执行 - Console.WriteLine("GetAvailableRoomsAsync method started"); - - // 暂时禁用缓存,直接从数据库获取 - Console.WriteLine("Bypassing cache, fetching directly from database"); + // 先从缓存获取 + var cachedRooms = await _cache.GetStringAsync("available_rooms"); + if (!string.IsNullOrEmpty(cachedRooms)) + { + return JsonSerializer.Deserialize>(cachedRooms); + } - // 从数据库获取 + // 缓存未命中,从数据库获取 var rooms = await _context.GameRooms .Include(r => r.Players) .Include(r => r.Spectators) .OrderByDescending(r => r.CreatedAt) .ToListAsync(); - // 调试日志:记录获取到的房间数量 - Console.WriteLine($"Fetched {rooms.Count} rooms from database"); + // 更新缓存 + try + { + await _cache.SetStringAsync( + "available_rooms", + JsonSerializer.Serialize(rooms), + new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromSeconds(10) } + ); + } + catch (RedisConnectionException ex) + { + Console.WriteLine($"Redis connection error: {ex.Message}"); + } return rooms; } diff --git a/frontend/jsconfig.json b/frontend/jsconfig.json deleted file mode 100644 index 5a1f2d222a302a174e710614c6d76531b7bda926..0000000000000000000000000000000000000000 --- a/frontend/jsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "compilerOptions": { - "paths": { - "@/*": ["./src/*"] - } - }, - "exclude": ["node_modules", "dist"] -} diff --git a/frontend/src/components/RoomList.vue b/frontend/src/components/RoomList.vue index 91ea86cd67661d742136b12a14d1b02d2c49419f..d4f8e729ce10ca1d90aa620be977fa824f51eb23 100644 --- a/frontend/src/components/RoomList.vue +++ b/frontend/src/components/RoomList.vue @@ -149,18 +149,7 @@ onUnmounted(() => { // 获取房间列表 function fetchRoomList() { console.log('Requesting room list from server...'); - // 添加超时处理,确保请求不会无限等待 - const timeoutId = setTimeout(() => { - console.error('GetRoomList request timed out'); - }, 5000); - - // 发送请求并处理响应 - socketService.emit('GetRoomList', {}, (response) => { - clearTimeout(timeoutId); - console.log('GetRoomList response received:', response); - // 手动调用处理函数 - handleRoomListUpdated(response); - }); + socketService.emit('GetRoomList'); } // 状态转换映射 @@ -172,9 +161,6 @@ const statusMap = { // 监听房间列表更新事件 function handleRoomListUpdated(response) { - // 调试:打印完整响应 - console.log('Received RoomListUpdated response:', response); - // 处理两种可能的响应格式 let updatedRooms = []; if (Array.isArray(response)) { @@ -189,14 +175,6 @@ function handleRoomListUpdated(response) { console.error('Invalid room list response:', response); } - // 调试:打印接收到的房间数量 - console.log(`Total rooms received: ${updatedRooms.length}`); - - // 如果有房间数据,打印第一个房间的详细信息 - if (updatedRooms.length > 0) { - console.log('First room details:', updatedRooms[0]); - } - // 转换房间状态为中文 updatedRooms = updatedRooms.map(room => ({ ...room, diff --git a/frontend/src/components/TestRoomList.vue b/frontend/src/components/TestRoomList.vue deleted file mode 100644 index 858f1df90026c097d5e29c5fa1dd38fdb40a3a27..0000000000000000000000000000000000000000 --- a/frontend/src/components/TestRoomList.vue +++ /dev/null @@ -1,105 +0,0 @@ - - - - - \ No newline at end of file diff --git a/frontend/src/services/socketService.js b/frontend/src/services/socketService.js index 389cf2423fde948f0dd79368d229dce1c77aba74..3496b59471a9e8d3123bac0151ca28f3e851360d 100644 --- a/frontend/src/services/socketService.js +++ b/frontend/src/services/socketService.js @@ -133,20 +133,6 @@ export class SocketService { // 发送消息 (调用SignalR方法) emit(methodName, ...args) { - // 保存原始回调(如果有) - let originalCallback = null; - if (args.length > 0 && typeof args[args.length - 1] === 'function') { - originalCallback = args.pop(); - } - - // 包装回调以添加更多调试信息 - const wrappedCallback = (result) => { - console.log(`Received response for ${methodName}:`, result); - if (originalCallback) { - originalCallback(result); - } - }; - // 确保连接已初始化 if (!this.connection) { console.error(`Cannot invoke ${methodName}: Connection not initialized. Call init() first.`); @@ -154,18 +140,10 @@ export class SocketService { this.init() .then(() => { console.log(`Connection initialized, invoking ${methodName}...`); - // 重新调用,带上包装后的回调 - if (originalCallback) { - this.emit(methodName, ...args, wrappedCallback); - } else { - this.emit(methodName, ...args); - } + this.emit(methodName, ...args); }) .catch(error => { console.error(`Failed to initialize connection when invoking ${methodName}:`, error); - if (originalCallback) { - originalCallback({ error: error.message }); - } }); return; } @@ -177,33 +155,24 @@ export class SocketService { this.init() .then(() => { console.log(`Connection restored, retrying ${methodName}...`); - // 重新调用,带上包装后的回调 - if (originalCallback) { - this.emit(methodName, ...args, wrappedCallback); - } else { - this.emit(methodName, ...args); - } + this.emit(methodName, ...args); }) .catch(error => { console.error(`Failed to reconnect when invoking ${methodName}:`, error); - if (originalCallback) { - originalCallback({ error: error.message }); - } }); return; } console.log(`Invoking SignalR method: ${methodName}`); - if (originalCallback) { + // 如果最后一个参数是函数,将其作为回调 + if (typeof args[args.length - 1] === 'function') { + const callback = args.pop(); this.connection.invoke(methodName, ...args) .then(result => { console.log(`Successfully invoked ${methodName}`); - wrappedCallback(result); + callback(result); }) - .catch(error => { - console.error(`Error invoking ${methodName}:`, error); - wrappedCallback({ error: error.message }); - }); + .catch(error => console.error(`Error invoking ${methodName}:`, error)); } else { this.connection.invoke(methodName, ...args) .then(() => console.log(`Successfully invoked ${methodName}`)) diff --git a/frontend/src/views/HomeView.vue b/frontend/src/views/HomeView.vue index 586f156f466950165e57f233e239d29fa797ec2f..1609678085728e41d3b83b935b150c033fd32c96 100644 --- a/frontend/src/views/HomeView.vue +++ b/frontend/src/views/HomeView.vue @@ -1,6 +1,5 @@