You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Startup from Server:
[code]
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using System.Linq;
namespace BlazorAndSignalR.Server
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddNewtonsoftJson();
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("http://localhost:51742");
}));
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBlazorDebugging();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
app.UseCors("CorsPolicy");
app.UseSignalR(routes => routes.MapHub<ChatHub>("/chathub"));
app.UseHttpsRedirection();
app.UseBlazor<Client.Startup>();
}
}
}
[/code]
Hub:
[code]
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace BlazorAndSignalR.Server
{
public class ChatHub : Hub
{
public override Task OnConnectedAsync()
{
if(string.IsNullOrEmpty(Context.ConnectionId))
{
throw new System.Exception(nameof(Context.ConnectionId)); //This code is never hit
}
Clients.All.SendAsync("broadcastMessage", "system", $"{Context.ConnectionId} joined the conversation");
return base.OnConnectedAsync();
}
public void Send(string name, string message)
{
Clients.All.SendAsync("broadcastMessage", name, message);
}
public override Task OnDisconnectedAsync(System.Exception exception)
{
Clients.All.SendAsync("broadcastMessage", "system", $"{Context.ConnectionId} left the conversation");
return base.OnDisconnectedAsync(exception);
}
}
Startup from Server:
[code]
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using System.Linq;
namespace BlazorAndSignalR.Server
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddNewtonsoftJson();
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
}
[/code]
Hub:
[code]
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace BlazorAndSignalR.Server
{
public class ChatHub : Hub
{
public override Task OnConnectedAsync()
{
if(string.IsNullOrEmpty(Context.ConnectionId))
{
throw new System.Exception(nameof(Context.ConnectionId)); //This code is never hit
}
Clients.All.SendAsync("broadcastMessage", "system", $"{Context.ConnectionId} joined the conversation");
return base.OnConnectedAsync();
}
public void Send(string name, string message)
{
Clients.All.SendAsync("broadcastMessage", name, message);
}
}
[/code]
Page:
[code]
@page "/chathub"
@foreach (var message in messages) {- @message
}
}
[/code]
The text was updated successfully, but these errors were encountered: