SignalR: WebSocket closed with status code: 1006 (no reason given) called while in state Connected

104 views Asked by At

How can i overcome this error? The way i use SignalR on client (Angular):

this.hubConnection = new HubConnectionBuilder()
      .withUrl(environment.appUrl + '/api/hub/weatherforecast', {
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets,
        logMessageContent: true
      })
      .configureLogging(signalR.LogLevel.Trace)
      .build();

    this.hubConnection
      .start()
      .then(() => console.log('SignalR connection started'))
      .catch(err => console.log('Error while starting SignalR connection: ' + err));

    this.hubConnection.onclose = (error) => {
      console.log('SignalR connection closing');
      console.log(error);
      window.location.reload();
    };

And on server:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<DeviceHub>("/api/hub/weatherforecast");
        });

Additionally i sniffed this traffic through WireShark:

enter image description here

1,3,5 requests addressed to server, 2 and 4 requests are sent from server. Logs from browser console:

[2024-03-04T11:16:23.996Z] Debug: Starting HubConnection.
[2024-03-04T11:16:23.996Z] Debug: Starting connection with transfer format 'Text'.
[2024-03-04T11:16:23.997Z] Trace: (WebSockets transport) Connecting.
SignalR connection refreshing
SignalR listeners added successfully
[2024-03-04T11:16:24.015Z] Information: WebSocket connected to ws://192.168.1.100:31234/api/hub/weatherforecast.
[2024-03-04T11:16:24.015Z] Debug: The HttpConnection connected successfully.
[2024-03-04T11:16:24.015Z] Debug: Sending handshake request.
[2024-03-04T11:16:24.016Z] Trace: (WebSockets transport) sending data. String data of length 32. Content: '{"protocol":"json","version":1}'.
[2024-03-04T11:16:24.016Z] Information: Using HubProtocol 'json'.
[2024-03-04T11:16:24.029Z] Trace: (WebSockets transport) data received. String data of length 3. Content: '{}'.
[2024-03-04T11:16:24.030Z] Debug: Server handshake complete.
[2024-03-04T11:16:24.030Z] Trace: (WebSockets transport) sending data. String data of length 11. Content: '{"type":6}'.
[2024-03-04T11:16:24.030Z] Debug: HubConnection connected successfully.
SignalR connection started
[2024-03-04T11:16:26.111Z] Trace: (WebSockets transport) socket closed.
[2024-03-04T11:16:26.111Z] Debug: HttpConnection.stopConnection(Error: WebSocket closed with status code: 1006 (no reason given).) called while in state Connected.
[2024-03-04T11:16:26.112Z] Error: Connection disconnected with error 'Error: WebSocket closed with status code: 1006 (no reason given).'.
[2024-03-04T11:16:26.112Z] Debug: HubConnection.connectionClosed(Error: WebSocket closed with status code: 1006 (no reason given).) called while in state Connected.

WebSockets are enabled in IIS, and my server is running correctly in debug, but when deployed on IIS, it cause this error. It literally connecting for a second and the next throws an error. It feels like something else is trying to end connection, because in server logs there are nothing special can be found:

04-03-2024 | 16:28:10.802 | INFO | Request starting HTTP/1.1 GET http://192.168.1.100:31234/api/hub/weatherforecast - -
04-03-2024 | 16:28:10.802 | DEBUG | The request path  does not match the path filter
04-03-2024 | 16:28:10.802 | DEBUG | 1 candidate(s) found for the request path '/api/hub/weatherforecast'
04-03-2024 | 16:28:10.802 | DEBUG | Request matched endpoint '/api/hub/weatherforecast'
04-03-2024 | 16:28:10.802 | DEBUG | The request has an origin header: 'http://localhost:5000'.
04-03-2024 | 16:28:10.802 | INFO | CORS policy execution successful.
04-03-2024 | 16:28:10.802 | INFO | Executing endpoint '/api/hub/weatherforecast'
04-03-2024 | 16:28:10.802 | DEBUG | New connection Fx_ljUj6zb5j2umX9pJeCQ created.
04-03-2024 | 16:28:10.802 | DEBUG | Establishing new connection.
04-03-2024 | 16:28:10.817 | DEBUG | OnConnectedAsync started.
04-03-2024 | 16:28:10.817 | DEBUG | Socket opened using Sub-Protocol: '(null)'.
04-03-2024 | 16:28:10.830 | DEBUG | Found protocol implementation for requested protocol: json.
04-03-2024 | 16:28:10.830 | DEBUG | Completed connection handshake. Using HubProtocol 'json'.

That's all. Client knows that something went wrong but cant describe anything, and server does not know anything about error. I've watched for proxy setting - there are none, nor VPN settings were used. What can i else watch for to determine the source of the problem and maybe how can i solve it?

0

There are 0 answers