我尝试为 ASP.NET Core 实现 SignalR,如下所述:https ://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration
但我总是得到一个错误。浏览器控制台输出:
DEBUG:
Starting connection using WebSockets transport
Information: Normalizing '/signalr-myChatHub' to 'http://localhost:62114/signalr-myChatHub'
SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request
我正在使用所有软件包 3.6.2 和 Abp.AspNetCore.SignalR 3.6.2-preview6 的最新版本。
Web.Host/Startup.cs:
#elif FEATURE_SIGNALR_ASPNETCORE
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr");
routes.MapHub<MyChatHub>("/signalr-myChatHub"); // Prefix with '/signalr'
});
#endif
复制粘贴MyChatHub.cs:
public class MyChatHub : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public MyChatHub()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
}
}
或多或少复制粘贴的视图:
@using EMetall.Web.Startup
@section scripts
{
<environment names="Development">
<script src="~/lib/signalr-client/signalr.min.js"></script>
<script src="~/lib/abp-web-resources/Abp/Framework/scripts/libs/abp.signalr-client.js"></script>
</environment>
Cats萌萌
相关分类