0

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() }));
        }
}
Ori
  • 16
  • 3
  • Does this answer your question? [getting the reason why websockets closed with close code 1006](https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006) – sellotape Jul 18 '21 at 13:46

1 Answers1

0

And for this issue, we need to know when chrome not match the websocket standard, it will show this error. So you can try to find more details by websocket.onerror(evt). But sometimes chrome will not report any close code 1006 reasons to the Javascript side. You can also use Firefox to do this operation, Firefox can report the details to Javascript side.

For more details, you can also refer to this post as Ori mentioned in comments.

By the way, if you use nginx, you can also have a try to set a long time value for proxy_read_timeout.

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Hello! I tried to debug it on Firefox as you suggest and these are the results: *error log from onerror event: * Error: WebSocket closed with status code: 1006 () *another error log from WebSocketTransport.js.pre-build-optimizer.js:99:44:* The connection to wss://some.url.com/triggers?id=WQeYB5Ek2WL0RuiCJ3ipCA was interrupted while the page was loading. – Ori Jul 23 '21 at 08:40
  • @Ori Could you please have a try with the solutions on this [post](https://ccm.net/forum/affich-57878-firefox-connection-interrupted). – Hury Shen Jul 23 '21 at 09:06