14

I use SignalR in an Angular app. When I destroy component in Angular I also want to stop connection to the hub. I use the command:

this.hubConnection.stop();

But I get an error in Chrome console: Websocket closed with status code: 1006

In Edge: ERROR Error: Uncaught (in promise): Error: Invocation canceled due to connection being closed. Error: Invocation canceled due to connection being closed.

It actually works and connection has been stopped, but I would like to know why I get the error.

This is how I start the hub:

this.hubConnection = new HubConnectionBuilder()
      .withUrl("/matchHub")
      .build();

    this.hubConnection.on("MatchUpdate", (match: Match) => {
      // some magic
    })

    this.hubConnection
      .start()
      .then(() => {
        this.hubConnection.invoke("SendUpdates");
      });

EDIT

I finally find the issue. Its caused by change streams from Mongo. If I remove the code from SendUpdates() method then OnDisconnected is triggered.

    public class MatchHub : Hub
    {
    private readonly IMatchManager matchManager;

    public MatchHub(IMatchManager matchManager)
    {
        this.matchManager = matchManager;
    }

    public async Task SendUpdates() {
        using (var changeStream = matchManager.GetChangeStream()) {
            while (changeStream.MoveNext()) {
                var changeStreamDocument = changeStream.Current.FullDocument;
                if (changeStreamDocument == null) {
                    changeStreamDocument = BsonSerializer.Deserialize<Match>(changeStream.Current.DocumentKey);
                }
                await Clients.Caller.SendAsync("MatchUpdate", changeStreamDocument);
            }
        }
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await base.OnDisconnectedAsync(exception);
    }
}

Method GetChangeStream from the manager.

        ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
        var watch =  mongoDb.Matches.Watch(options).ToEnumerable().GetEnumerator();
        return watch;

But I don't know how to fix it.

Milad
  • 27,506
  • 11
  • 76
  • 85
EdWood
  • 875
  • 3
  • 19
  • 37
  • 1
    Connections are opened by javascript, but javascript does not continue. The native side of the browser handles this. When you send the close order, the browser handles this order asynchronously and launches an notification of the result. – Erçin Dedeoğlu Jan 02 '19 at 08:49
  • having the same weird issue. Do you find the answer? – maxisam Jan 24 '19 at 19:09
  • I had a bad design of hub. I moved changStream (everything from SendUpdates method) to a different class and from this class I call the hub.Clients.All.SendAsync... through dependency injection. – EdWood Jan 28 '19 at 18:10

2 Answers2

1

This can be for many reasons but i think it is most likely this one:

I think this is because of how the server is handling the connected / disconnected events. I can't say for sure but the connection closing needs to handled correctly on the server also with code. Try overriding the built in On Connected /Disconnected methods on the server and see. My assumption only is that you're closing it but the server isn't closing properly and therefore not relaying the proper closed response.

found as a comment at : getting the reason why websockets closed with close code 1006

Where you don't need to change the connection/disconection because evrything works fine. But as an answer this one is the most likely.

Seppe Mariën
  • 355
  • 1
  • 13
  • I tried to override OnDisconnectedAsync which should be probably called by stop() method in javascript, but in my case, OnDisconnectedAsync is never called. – EdWood Jan 01 '19 at 22:00
  • That could be the problem you are facing! Maybe the stop() method does not disconnect and that's why you get the error: The hubConnection get closed without disconnecting properly. – Seppe Mariën Jan 05 '19 at 16:32
0

It throws error because the callback doesn't get clear properly.

And it is caused by the return data from websocket.

normally it should return like

enter image description here

However, for some reason it might return something like

enter image description here

the very last response breaking into 2 pieces

And that causes the issue.

I don't think there is a way to bypass this without changing the source code.

I reported this on github repo as well at here

It turns out that I can just utilize invocation response to notify client to stop the hub. So it doesn't trigger racing issue.

maxisam
  • 21,975
  • 9
  • 75
  • 84