envoi et creation messages
This commit is contained in:
+10
-6
@@ -4,7 +4,7 @@ using Microsoft.AspNetCore.SignalR;
|
|||||||
namespace Knots.Hubs;
|
namespace Knots.Hubs;
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class ChatHub : Hub
|
public class ChatHub(KnotsDbContext db, AutoMapper.IMapper mapper) : Hub
|
||||||
{
|
{
|
||||||
// Rejoindre une conversation (room)
|
// Rejoindre une conversation (room)
|
||||||
public async Task JoinConversation(string conversationId)
|
public async Task JoinConversation(string conversationId)
|
||||||
@@ -21,14 +21,18 @@ public class ChatHub : Hub
|
|||||||
// Envoyer un message à une conversation
|
// Envoyer un message à une conversation
|
||||||
public async Task SendMessage(string conversationId, string content)
|
public async Task SendMessage(string conversationId, string content)
|
||||||
{
|
{
|
||||||
var message = new
|
var message = new Models.Message
|
||||||
{
|
{
|
||||||
SenderId = Context.UserIdentifier,
|
// adapte aux noms réels de ton modèle (AuthorId, Contenu, Date...)
|
||||||
Content = content,
|
AuthorId = int.Parse(Context.UserIdentifier!),
|
||||||
SentAt = DateTime.UtcNow
|
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);
|
await Clients.Group(conversationId).SendAsync("ReceiveMessage", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ public class Message
|
|||||||
[Required] public DateTime Date { get; set; }
|
[Required] public DateTime Date { get; set; }
|
||||||
[Required] public Boolean Type { get; set; }
|
[Required] public Boolean Type { get; set; }
|
||||||
|
|
||||||
|
public int AuthorId { get; set; }
|
||||||
|
|
||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
public User User { get; set; } = null!;
|
public User User { get; set; } = null!;
|
||||||
|
|
||||||
|
|||||||
+20
-3
@@ -21,7 +21,8 @@ builder.Services.AddCors(options =>
|
|||||||
policyBuilder
|
policyBuilder
|
||||||
.WithOrigins("http://localhost:5250", "http://localhost:4200")
|
.WithOrigins("http://localhost:5250", "http://localhost:4200")
|
||||||
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
|
.WithMethods("GET", "POST", "PUT", "PATCH", "DELETE")
|
||||||
.AllowAnyHeader();
|
.AllowAnyHeader()
|
||||||
|
.AllowCredentials();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -47,7 +48,24 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||||||
ValidAudience = builder.Configuration["Jwt:Audience"],
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
||||||
IssuerSigningKey = new SymmetricSecurityKey(
|
IssuerSigningKey = new SymmetricSecurityKey(
|
||||||
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
|
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();
|
builder.Services.AddAuthorization();
|
||||||
|
|
||||||
@@ -58,8 +76,6 @@ builder.Services.AddAutoMapper(cfg => { }, typeof(Program).Assembly);
|
|||||||
// On construit l'application en lui donnant vie
|
// On construit l'application en lui donnant vie
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
app.MapHub<ChatHub>("hubs/chat");
|
|
||||||
|
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
@@ -73,5 +89,6 @@ app.UseAuthentication()
|
|||||||
}
|
}
|
||||||
).UseSwaggerGen();
|
).UseSwaggerGen();
|
||||||
|
|
||||||
|
app.MapHub<ChatHub>("hubs/chat");
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Knots")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("Knots")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[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.AssemblyProductAttribute("Knots")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("Knots")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("Knots")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[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.pdb
|
||||||
C:\Users\dogge\RiderProjects\Knots\Knots\obj\Debug\net8.0\Knots.genruntimeconfig.cache
|
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\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.
Reference in New Issue
Block a user