envoi et creation messages

This commit is contained in:
gokhoal
2026-06-11 00:44:28 +02:00
parent 4f994ba183
commit e6021dcc61
16 changed files with 36 additions and 12 deletions
+10 -6
View File
@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.SignalR;
namespace Knots.Hubs;
[Authorize]
public class ChatHub : Hub
public class ChatHub(KnotsDbContext db, AutoMapper.IMapper mapper) : Hub
{
// Rejoindre une conversation (room)
public async Task JoinConversation(string conversationId)
@@ -21,14 +21,18 @@ public class ChatHub : Hub
// Envoyer un message à une conversation
public async Task SendMessage(string conversationId, string content)
{
var message = new
var message = new Models.Message
{
SenderId = Context.UserIdentifier,
Content = content,
SentAt = DateTime.UtcNow
// adapte aux noms réels de ton modèle (AuthorId, Contenu, Date...)
AuthorId = int.Parse(Context.UserIdentifier!),
Contenu = content,
Date = DateTime.UtcNow,
DiscussionId = int.Parse(conversationId)
};
// Diffuse à tous les membres de la conversation
db.Messages.Add(message);
await db.SaveChangesAsync();
await Clients.Group(conversationId).SendAsync("ReceiveMessage", message);
}
+2
View File
@@ -9,6 +9,8 @@ public class Message
[Required] public DateTime Date { get; set; }
[Required] public Boolean Type { get; set; }
public int AuthorId { get; set; }
public int UserId { get; set; }
public User User { get; set; } = null!;
+20 -3
View File
@@ -21,7 +21,8 @@ builder.Services.AddCors(options =>
policyBuilder
.WithOrigins("http://localhost:5250", "http://localhost:4200")
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
.AllowAnyHeader();
.AllowAnyHeader()
.AllowCredentials();
});
});
@@ -47,7 +48,24 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
path.StartsWithSegments("/hubs"))
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization();
@@ -58,8 +76,6 @@ builder.Services.AddAutoMapper(cfg => { }, typeof(Program).Assembly);
// On construit l'application en lui donnant vie
WebApplication app = builder.Build();
app.MapHub<ChatHub>("hubs/chat");
app.UseCors();
app.UseHttpsRedirection();
@@ -73,5 +89,6 @@ app.UseAuthentication()
}
).UseSwaggerGen();
app.MapHub<ChatHub>("hubs/chat");
app.Run();
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Knots")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0b9e01c92578a51eb81cd673aaae95ac56d7c4af")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4f994ba183ab4a079dd7e762d3b7cacca26f8f75")]
[assembly: System.Reflection.AssemblyProductAttribute("Knots")]
[assembly: System.Reflection.AssemblyTitleAttribute("Knots")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
b62edb6f2119aec2fe98bcce877c4ef16135c025b03ceefc31d7509622b283ee
aa1c1119a88f3e3b5738f6ea6f67686474d5c6d7e7b2612859a07f3ee65ef199
@@ -1 +1 @@
6edaa06a6de0e0e58dbcac8a2a5db11b7c3fa5582c067ae51bfae956cf647dc5
bab9e1ed6ec5ae3422f4377cfa8a1795b3c9881cbfdada74a84426e15b5819ff
@@ -518,3 +518,4 @@ C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\refint\Knots.dll
C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\Knots.pdb
C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\Knots.genruntimeconfig.cache
C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\ref\Knots.dll
C:\Users\dogge\RiderProjects\Knots\Knots\bin\Debug\net8.0\System.Net.WebSockets.WebSocketProtocol.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.