From e48eb6f76b6c967f5c916988bfb25fcd351aeb64 Mon Sep 17 00:00:00 2001 From: Seonkyu_Kim Date: Mon, 17 Feb 2025 15:35:58 +0900 Subject: [PATCH] =?UTF-8?q?[=E2=9C=A8]=20SignalR=EC=9D=84=20=ED=99=9C?= =?UTF-8?q?=EC=9A=A9=ED=95=9C=20=EC=B1=84=ED=8C=85=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4(=EA=B8=B0=EC=B4=88)=20=EA=B0=9C=EB=B0=9C=20=EB=B0=8F?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 42 ++++++++++++++++++++++++++-------- Program/Common/Chat/ChatHub.cs | 37 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 Program/Common/Chat/ChatHub.cs diff --git a/Program.cs b/Program.cs index b44b393..f470461 100644 --- a/Program.cs +++ b/Program.cs @@ -1,12 +1,10 @@ - using Pomelo.EntityFrameworkCore; - using System.Text; - +using AcaMate.Common.Chat; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; - +using Microsoft.AspNetCore.SignalR; using AcaMate.Common.Models; using AcaMate.V1.Services; using AcaMate.Common.Data; @@ -15,9 +13,6 @@ using AcaMate.V1.Controllers; var builder = WebApplication.CreateBuilder(args); - - - // DB 설정부 시작 builder.Configuration.AddJsonFile("private/dbSetting.json", optional: true, reloadOnChange: true); // var connectionString = builder.Configuration.GetConnectionString("MariaDbConnection"); @@ -31,7 +26,7 @@ builder.Services.AddDbContext(optionsAction: (serviceProvider, opt { baseConnectionString = baseConnectionString.Replace("database=AcaMate", $"database={dbName}"); } - + options.UseMySql(baseConnectionString, ServerVersion.AutoDetect(baseConnectionString)); }); builder.Services.AddHttpContextAccessor(); @@ -53,7 +48,8 @@ builder.Services.AddAuthentication(options => .AddJwtBearer(options => { var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get(); - options.TokenValidationParameters = new TokenValidationParameters { + options.TokenValidationParameters = new TokenValidationParameters + { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, @@ -76,6 +72,23 @@ builder.Services.AddEndpointsApiExplorer(); // builder.Services.AddSwaggerGen(); builder.Services.AddCustomSwagger(); +// SignalR 설정 추가 부분 +builder.Services.AddSignalR(); +builder.Services.AddCors(option => +{ + option.AddPolicy("CorsPolicy", builder => + { + builder + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials() + .SetIsOriginAllowed(host => true); + }); +}); + + +///// ===== builder 설정 부 ===== ///// + var app = builder.Build(); if (app.Environment.IsDevelopment()) @@ -84,7 +97,6 @@ if (app.Environment.IsDevelopment()) // app.UseSwaggerUI(); app.UseCustomSwaggerUI(); app.UseDeveloperExceptionPage(); // 좀더 자세한 예외 정보 제공 - } else { @@ -98,4 +110,14 @@ app.UseAuthorization(); app.MapControllers(); +app.UseCors("CorsPolicy"); +app.UseRouting(); + +app.UseWebSockets(); +app.UseEndpoints(end => +{ + app.MapHub("/chatHub"); +}); + + app.Run(); \ No newline at end of file diff --git a/Program/Common/Chat/ChatHub.cs b/Program/Common/Chat/ChatHub.cs new file mode 100644 index 0000000..9841c96 --- /dev/null +++ b/Program/Common/Chat/ChatHub.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.SignalR; +using System.Threading.Tasks; + +namespace AcaMate.Common.Chat; + +public class ChatHub : Hub +{ + // 클라이언트에서 메시지를 보내면 모든 사용자에게 전송 + public async Task SendMessage(string user, string message) + { + Console.WriteLine($"Message received: {user}: {message}"); + await Clients.All.SendAsync("ReceiveMessage", user, message); + + } + + // 특정 사용자에게 메시지를 보냄 + public async Task SendMessageToUser(string connectionId, string message) + { + await Clients.Client(connectionId).SendAsync("ReceiveMessage", message); + } + + // 클라이언트가 연결될 때 호출 + public override async Task OnConnectedAsync() + { + await Clients.Caller.SendAsync("ReceiveMessage", "System", $"Welcome! Your ID: {Context.ConnectionId}"); + Console.WriteLine("OnConnectedAsync"); + await base.OnConnectedAsync(); + } + + // 클라이언트가 연결 해제될 때 호출 + public override async Task OnDisconnectedAsync(Exception? exception) + { + await Clients.All.SendAsync("ReceiveMessage", "System", $"{Context.ConnectionId} disconnected"); + Console.WriteLine("OnDisconnectedAsync"); + await base.OnDisconnectedAsync(exception); + } +} \ No newline at end of file