Call specific client from SignalR Call specific client from SignalR asp.net asp.net

Call specific client from SignalR


$('#sendmessage').click(function () {    // Call the Send method on the hub.     chat.server.send($('#displayname').val(), $('#message').val(), $.connection.hub.id);    // Clear text box and reset focus for next comment.     $('#message').val('').focus();});

at server side send the id of the client and response to that id

  public void Send ( string name , string message , string connID )  {        Clients.Client(connID).broadcastMessage(name , message);  }


Every time you send a request to the hub server, your request will have a different connection id, so, I added a static hash table that contains a username- which is not changing continuously, and a connection id fro the signal r,every time you connect, the connection id will be updated

 $.connection.hub.start().done(function () {   chat.server.registerConId($('#displayname').val()); });

and in the server code:

public class ChatHub : Hub{    private static Hashtable htUsers_ConIds = new Hashtable(20);    public void registerConId(string userID)    {        if(htUsers_ConIds.ContainsKey(userID))            htUsers_ConIds[userID] = Context.ConnectionId;        else            htUsers_ConIds.Add(userID, Context.ConnectionId);    }}