Vinyl Warmth
Vinyl Warmth

Reputation: 2506

Client not receiving messages in SignalR 2.2 app

In my application I want to update clients when a user signs in but clients are not receiving messages from the Hub.

My Hub class:

    public class GameHub : Hub
    {
        public async Task UserLoggedIn(string userName)
        {
            await Clients.All.SendAsync("UserLoggedIn", userName);
        }
    }

UserLoggedIn is executed when a user logs in, I set a breakpoint and the UserLoggedIn method on GameHub is definitely called.

On the client page I have

        const connection = new signalR.HubConnectionBuilder()
            .withUrl("/gameHub")
            .configureLogging(signalR.LogLevel.Information)
            .build();

        connection.start().then(() => {
            console.log("connected");
        });

        connection.on("UserLoggedIn", (userName) => {
            console.log("User Logged In" + userName);
        });

In a console window in another incognito browser I see "connected" but I don't see "User Logged In" after that method is invoked on GameHub

Can anyone see what I'm doing wrong here?

Upvotes: 1

Views: 2960

Answers (1)

Tony
Tony

Reputation: 20082

You don't received message for incognito browser because you only listen on signalr not invoke it so I would change your code into this

public class GameHub : Hub
{
    public async Task UserLoggedIn(string userName)
    {
        await Clients.All.SendAsync("SendUserLoggedInMessage", userName);
    }
}

Then in js code

<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/gameHub")
        .configureLogging(signalR.LogLevel.Information)
        .build();

    connection.start().then(() => {
        console.log("connected");
    });

    // listen on SendUserLoggedInMessage
    connection.on("SendUserLoggedInMessage", (userName) => {
        console.log("User Logged In" + userName);
    });

    // invoke UserLoggedIn method
   connection.send("userLoggedIn", username)
              .then(() => console.log("something"));

</script>

Upvotes: 1

Related Questions