角度信号R经常与错误状态代码1006断开连接



我的signalR有一些问题,我找不到哪里出了问题。我可以建立连接并获取事件,但每次客户端随机断开连接时都会出现以下错误:;错误:连接已断开,出现错误"错误:Websocket已关闭,状态代码:1006((">

我在angular和AspNetCore.core 5 signalr上使用@microsoft/signar 5.0.8版本。该网站托管在IIS 10中。

客户端代码:

createSignalrConnection(){
var comp = this;
this.signalR = new signalR.HubConnectionBuilder()
.withUrl(environment.signalrEndpoint + "/triggers")
.withAutomaticReconnect()
.build();

this.signalR.start();
this.signalR.onreconnecting(error=>{
console.log(error.toString(), " reconnect..");
});
this.signalR.on('TriggerMessage', (message: string)=>{
try{
comp.eventManager.emit("triggerEvent", machine);
}
catch(err)
{
console.log(err.toString());
}
});
}

服务器端代码:

启动.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
});
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory){
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<TriggersHub>("/triggers");
});
}

触发器Hub.cs

public class TriggersHub: Microsoft.AspNetCore.SignalR.Hub
{
public async Task SendMessage(MachineJitData message)
{
await Clients.All.SendAsync("TriggerMessage", JsonConvert.SerializeObject(message));
}
}

事件控制器.cs

public class EventsController
{
protected ILoggerService _logger { get; }
protected AppSettings _appSettings { get; }
protected IDataService _dataService { get; }
protected IHubContext<TriggersHub> _hubContext { get; }
public EventsController(IOptions<AppSettings> appSettings, ILoggerService logger, IDataService dataService, IHubContext<TriggersHub> hubContext) : base(appSettings)
{
_appSettings = appSettings.Value;
_logger = logger;
_dataService = dataService;
_hubContext = hubContext;
}
[HttpPost]
[Route("data-trigger")]
public async Task GetMonitorRefreshTrigger([FromBody] MachineJitData data)
{
var new_jit_data = await _dataService.UpdateMachineJitData(data);
await _hubContext.Clients.All.SendAsync("TriggerMessage", JsonConvert.SerializeObject(new_jit_data, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }));
}
}

对于这个问题,我们需要知道当chrome与websocket标准不匹配时,它会显示这个错误。因此,您可以尝试通过websocket.onerror(evt)查找更多详细信息。但有时chrome不会向Javascript端报告任何关闭代码1006的原因。您也可以使用Firefox来做此操作,Firefox可以向Javascript端报告详细信息。

有关更多详细信息,您也可以参考Ori在评论中提到的这篇文章。

顺便说一句,如果你使用nginx,你也可以尝试为proxy_read_timeout设置一个长时间值。

最新更新