Check if User with specific identifier exists while using SignalR

Viewed 17

I am using SignalR to send a message to my client. The way I am doing this is in a static function inside my hub file:

    public static void CallTestFunction(int UserID, short TestParam)
    {
        try
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
            hubContext.Clients.User(UserID.ToString()).TestFunction(TestParam);
        }
        catch (Exception ex)
        {
            Logger.LogError(ex);
        }
    }

This works prefectly fine. However, this does not throw error if I pass a random userID to this function like "123456789". What I want to do is something like:

    public static void CallTestFunction(int UserID, short TestParam)
    {
        try
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
            if(// Some sort of check like hubContext.Clients.User.Exists(UserID.ToString())
              hubContext.Clients.User(UserID.ToString()).TestFunction(TestParam);
            else
              //Throw exception saying that user does not exist.
        }
        catch (Exception ex)
        {
            Logger.LogError(ex);
        }
    }

What do I do in the second code's if statement to make that code work?

0 Answers
Related