1
votes

I'm trying to create a chat application using SignalR. To make it possible to send private messages I want to assign clients to a group with the name of their profileID. So I can simply call the addMessage function of the group to send to a specific client.

When I go to this page: https://github.com/SignalR/SignalR/wiki/Hubs

It tells me to add a function to the Hub called Join(). In here I can add the incomming client to a group. So I created this code:

    [HubName("Chat")]
    public class ChatHub : Hub
    {
            public Task Join()
            {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                            Profiel_DataHelper profiel = new Profiel_DataHelper(HttpContext.Current.User.Identity.Name);
                            return Groups.Add(Context.ConnectionId, profiel.ProfielID.ToString());
                    }
                    else
                    {
                            return null;
                    }
            }

.....

When I want to call a specific client I use this code:

    var context = GlobalHost.ConnectionManager.GetHubContext();
    context.Clients.Group(profielidNaar).addTyptOnline(profielidVan);

But when I run the program the Join() Task is not being called at all, therefore my call to the group is also not working.

What am I doing wrong?

1

1 Answers

1
votes

Join is a method on your hub that you need to call from the client. Nobody is going to call it for you and "Join" isn't a special method that gets called automatically. The documentation is showing you how to declare method that "can" be called from the client.

There's other ways to know when clients connect, reconnect and disconnect and it's detailed here: https://github.com/SignalR/SignalR/wiki/Hubs#detecting-connect-reconnect-and-disconnect-clients-in-hubs