The Rest of the post
Asp net core. Program.cs
//Code...
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "Angular",
builder =>
{
builder.WithOrigins("https://localhost:44376", "https://localhost:7231")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin();
});
});
// Code...
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.MapHub<ChatHub>("Chat");
app.Run();
ChatHubs.cs
using Microsoft.AspNetCore.SignalR;
namespace DeliveryRestaurant.Hubs
{
public class ChatHub : Hub
{
public async Task SendAll(string message)
{
await Clients.All.SendAsync("ReceiveAllMessage", message);
}
Angular Ts
ngOnInit(): void {
let connection = new signalR.HubConnectionBuilder()
.withUrl("Chat")
.build();
connection.on("ReceiveAllMessage", data => {
console.log("REceived Data", data);
});
connection.start()
.then(() => connection.invoke("ReceiveAllMessage", "Hello"));
} }