Help with SignalR send to specific client and not everyone

jsniper

New member
Joined
Feb 19, 2015
Messages
1
Programming Experience
1-3
I downloaded this example https://code.msdn.microsoft.com/Using-SignalR-in-WinForms-f1ec847b

This is a win forms app.

On the server side there is the following code which sends any messages received to all connected clients:

C#:
[COLOR=blue]public[/COLOR] [COLOR=blue]class[/COLOR] [COLOR=#2B91AF]MyHub[/COLOR] : [COLOR=#2B91AF]Hub[/COLOR]
    {
        [COLOR=blue]public[/COLOR] [COLOR=blue]void[/COLOR] Send([COLOR=blue]string[/COLOR] name, [COLOR=blue]string[/COLOR] message)
        {
            Clients.All.addMessage(name, message);
        }
        [COLOR=blue]public[/COLOR] [COLOR=blue]override[/COLOR] [COLOR=#2B91AF]Task[/COLOR] OnConnected()
        {
            [COLOR=#2B91AF]Program[/COLOR].MainForm.WriteToConsole([COLOR=#A31515]"Client connected: "[/COLOR] + Context.ConnectionId);
            [COLOR=blue]return[/COLOR] [COLOR=blue]base[/COLOR].OnConnected();
        }
        [COLOR=blue]public[/COLOR] [COLOR=blue]override[/COLOR] [COLOR=#2B91AF]Task[/COLOR] OnDisconnected()
        {
            [COLOR=#2B91AF]Program[/COLOR].MainForm.WriteToConsole([COLOR=#A31515]"Client disconnected: "[/COLOR] + Context.ConnectionId);
            [COLOR=blue]return[/COLOR] [COLOR=blue]base[/COLOR].OnDisconnected();
        }
    }

On the client side there is this code for sending the message:

private void ButtonSend_Click(object sender, EventArgs e)
{
HubProxy.Invoke("Send", UserName, TextBoxMessage.Text);
TextBoxMessage.Text = String.Empty;
TextBoxMessage.Focus();
}

Being a newbie and all especially to C Sharp, I don't know how to send messages to a specific client. Are there any SignalR gurus out there that can help or maybe take a look at SignalR. I found this at StackOverflow:

If you want to send message to a specific user, you can use the new method in version 2.0
Clients.User(userId).send(message);by default, the user id used is the IPrincipal.Identity.Name. If you want to override the user id mechanism with your own mapping, refer to this reply - asp.net - SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0* - Stack Overflow

Thank you in advance.
 
Top Bottom