Compare commits

..

No commits in common. "25dd659291a27ba7990f78a5a0a97e10a561bbfe" and "5ca2ad379b9689a757764e50551fb055712d65a1" have entirely different histories.

2 changed files with 10 additions and 69 deletions

View File

@ -1,10 +1,12 @@
using Pomelo.EntityFrameworkCore; using Pomelo.EntityFrameworkCore;
using System.Text; using System.Text;
using AcaMate.Common.Chat;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.SignalR;
using AcaMate.Common.Models; using AcaMate.Common.Models;
using AcaMate.V1.Services; using AcaMate.V1.Services;
using AcaMate.Common.Data; using AcaMate.Common.Data;
@ -13,6 +15,9 @@ using AcaMate.V1.Controllers;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// DB 설정부 시작 // DB 설정부 시작
builder.Configuration.AddJsonFile("private/dbSetting.json", optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile("private/dbSetting.json", optional: true, reloadOnChange: true);
// var connectionString = builder.Configuration.GetConnectionString("MariaDbConnection"); // var connectionString = builder.Configuration.GetConnectionString("MariaDbConnection");
@ -26,7 +31,7 @@ builder.Services.AddDbContext<AppDbContext>(optionsAction: (serviceProvider, opt
{ {
baseConnectionString = baseConnectionString.Replace("database=AcaMate", $"database={dbName}"); baseConnectionString = baseConnectionString.Replace("database=AcaMate", $"database={dbName}");
} }
options.UseMySql(baseConnectionString, ServerVersion.AutoDetect(baseConnectionString)); options.UseMySql(baseConnectionString, ServerVersion.AutoDetect(baseConnectionString));
}); });
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
@ -48,8 +53,7 @@ builder.Services.AddAuthentication(options =>
.AddJwtBearer(options => .AddJwtBearer(options =>
{ {
var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get<JwtSettings>(); var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get<JwtSettings>();
options.TokenValidationParameters = new TokenValidationParameters options.TokenValidationParameters = new TokenValidationParameters {
{
ValidateIssuer = true, ValidateIssuer = true,
ValidateAudience = true, ValidateAudience = true,
ValidateLifetime = true, ValidateLifetime = true,
@ -72,23 +76,6 @@ builder.Services.AddEndpointsApiExplorer();
// builder.Services.AddSwaggerGen(); // builder.Services.AddSwaggerGen();
builder.Services.AddCustomSwagger(); 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(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
@ -97,6 +84,7 @@ if (app.Environment.IsDevelopment())
// app.UseSwaggerUI(); // app.UseSwaggerUI();
app.UseCustomSwaggerUI(); app.UseCustomSwaggerUI();
app.UseDeveloperExceptionPage(); // 좀더 자세한 예외 정보 제공 app.UseDeveloperExceptionPage(); // 좀더 자세한 예외 정보 제공
} }
else else
{ {
@ -110,14 +98,4 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.UseCors("CorsPolicy");
app.UseRouting();
app.UseWebSockets();
app.UseEndpoints(end =>
{
app.MapHub<ChatHub>("/chatHub");
});
app.Run(); app.Run();

View File

@ -1,37 +0,0 @@
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);
}
}