I'm having some issues with signalR and I cant find what is wrong. I can establish connection and get events but every while the client side randomly disconnect with this error : "Error: Connection disconnected with error 'Error : Websocket closed with status code: 1006().'"
I'm using @microsoft/signalr version 5.0.8 on angular and AspNetCore .core 5 SignalR. the website is hosted in IIS 10.
client side code :
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());
}
});
}
server side code:
Startup.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");
});
}
TriggersHub.cs
public class TriggersHub: Microsoft.AspNetCore.SignalR.Hub
{
public async Task SendMessage(MachineJitData message)
{
await Clients.All.SendAsync("TriggerMessage", JsonConvert.SerializeObject(message));
}
}
EventsController.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() }));
}
}