1
votes

So its happened for the second time now. I believe what triggered the error was when I tried to do an update to my MongoDb server but I still don't know why this happens and I'd like to find out.

Basically I am sending json string data from a C# script to my front end with Signalr using this command:

_hubContext.Clients.All.SendAsync("ReceiveMail", json);

The issue is that my script keeps broadcasting this message (without any errors or issues) but my client side doesnt receive it (even though this broadcast has worked for weeks....). When I change the name of the broadcast to something different the data then makes its way to the client side perfectly.

Example:

//This broadcast worked fine for weeks but suddenly stopped working (without error)    
_hubContext.Clients.All.SendAsync("ReceiveMail", json);

//Changed above broadcast to this and broadcast works perfectly fine again    
_hubContext.Clients.All.SendAsync("ListenForMail", json);

TS Code:

constructor() {
    this.hubConnection = new signalR.HubConnectionBuilder()
        .withUrl('http://localhost:57697/chat')
        .build();

    this.hubConnection
        .start()
        .then(() => this.table())
        .catch(err => console.log('Error while establishing connection :('));

    this.hubConnection.on('ReceiveMail', (mailJson: string) => {
        this.loadEmail(mailJson);
    });

    this.hubConnection.on('ReceiveConnection', (msg: string) => {
        console.log('Connection: ' + msg);
    });
}

Anyone have any insight into this issue?

1
Can you post your JS code that is listening for the message please? - Simply Ged
@SimplyGed Updated with TS code - Alex Wenger

1 Answers

2
votes

The method name the C# code is calling doesn't match the methods you are listening for in the TS code - but I'll assume that's a typo. If it's not, then you need to make sure the .on methods use the same method names as the C# code.

Another thing you do need to change is where you start the connection E.G.

constructor() {
    this.hubConnection = new signalR.HubConnectionBuilder()
        .withUrl('http://localhost:57697/chat')
        .build();

    this.hubConnection.on('RetrieveMail', (mailJson: string) => {
        this.loadEmail(mailJson);
    });

    this.hubConnection.on('ReceiveConnection', (msg: string) => {
        console.log('Connection: ' + msg);
    });

    this.hubConnection
        .start()
        .then(() => this.table())
        .catch(err => console.log('Error while establishing connection :('));
}

In the code above I have moved the .start() call to AFTER registering the on methods. You should do it this way because the hubconnection can start listening messages before the handlers are registered causing a race condition. Any messages sent won't be received if the on handler hasn't finished registering.