我正在尝试在Nopcommerce 4.1插件上实现SignalR。但是出现以下错误。
WebSocket与'wss:// localhost:44356 / message?id = P70y4-FWNVPuLVFhKTaaKQ'的连接失败:WebSocket握手期间出错:意外的响应代码:404
无法启动传输“ WebSockets”:未定义
无法启动连接:错误:无法初始化任何可用的传输。
未捕获(承诺)错误:无法初始化任何可用的传输。
下面是集线器启动。下面的方法在每次启动时都会调用。
public class AnnouncementHubAtStartUp : INopStartup
{
public int Order => int.MaxValue;
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder application)
{
application.UseFileServer();
application.UseSignalR(routes =>
{
routes.MapHub<MessageHub>("/message");
});
}
}
下面是我的中心类
public class MessageHub : Hub
{
public Task Send(string message)
{
return Clients.All.SendAsync("Send", message);
}
}
我的控制者是
public class LiveAnnouncementController : BaseAdminController
{
private IHubContext<MessageHub> _messageHubContext;
public LiveAnnouncementController(IHubContext<MessageHub> messageHubContext)
{
_messageHubContext = messageHubContext;
}
[HttpPost]
public IActionResult Announcement(AnnouncementModel model)
{
AnnouncementDomain objOfAnnouncementDomain = new AnnouncementDomain();
objOfAnnouncementDomain.Name=model.Name;
objOfAnnouncementDomain.Body=model.Body;
objOfAnnouncementDomain.IsActive=model.IsActive;
objOfAnnouncementDomain.ShowingToDate=model.ShowingToDate;
objOfAnnouncementDomain.CreateDate = DateTime.UtcNow;
_announcementService.Insert(objOfAnnouncementDomain);
if (model.IsActive == true)
{
_messageHubContext.Clients.All.SendAsync("send", model.Body.ToString() +
DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
}
return RedirectToAction("AnnouncementList");
}
}
答案 0 :(得分:2)
我需要减少AnnouncementHubAtStartUp的order属性。因为下面的扩展方法
application.UseSignalR(routes =>
{
routes.MapHub<MessageHub>("/message");
});
应在NopMvcStartup
之前执行。
因此,订单必须小于1000。如果我更改订单999,则集线器将按预期方式连接。
更改的代码是
public class AnnouncementHubAtStartUp : INopStartup
{
public int Order => 999;
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder application)
{
application.UseSignalR(routes =>
{
routes.MapHub<MessageHub>("/message");
});
}
}