2

i am new with asp.net mvc and signalr. I send message to all clients in my message app but i want private messaging and in this article under the title "private chat" codes are like belove:

public void SendPrivateMessage(string toUserId, string message)
{

    string fromUserId = Context.ConnectionId;
    var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId) ;
    var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

    if (toUser != null && fromUser!=null)
    {
       // send to
       Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message);

       // send to caller user
       Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);
    }
}

question.1: Does fromUserId have to be a number or id not a name? question.2: Where did Connectedusers defined?because i am getting error about it. And here is my code that sends messages to all clients:

public void Send(string alici,string name,string message)
{
    Clients.All.addNewMessageToPage(name, message);
    //alici:name of receiver, name:name of sender
}

Can i use alici to send message to a specific user and how? thanks

5
  • Are you starting the hub when the page loads. Aug 24, 2015 at 12:12
  • Yes, i guess, because i can send to all clients @Murtaza Munshi Aug 24, 2015 at 12:33
  • Ok so are you maintaining a table or something like that for your users Aug 24, 2015 at 12:58
  • Yes, ConnectedUsers is a list of UserDetail that we don't see here, but if you grab the source code from the article that he is following, you will see there.
    – radu-matei
    Aug 24, 2015 at 12:59
  • @mervedemir Is this helpful in any way? Cheers!
    – radu-matei
    Aug 25, 2015 at 9:40

2 Answers 2

2

Answer for Question 1:

toUserId must me ID that will be almost similar to GUID (Uniqueidentifier), this will help you identify each user separately even if there will be two Users with same name.

Question 2 is answered by radu-matei and thats correct by far my knowledge.

1

ConnectedUsers is a list of UserDetail, which holds all the connected users. Now, you can either add them in the list when they connect to the hub (so you override the OnConnected method, either you create a server function, in this case Connect, which takes a string userName as parameter.(the userName that the user sets when first entering the page)).

Now, the userId is the SignalR ConnectionId that every client recieves when connecting the hub.

    public void Connect(string userName)
{

   var id = Context.ConnectionId;

   if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
   {

       ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });

       // send to caller
       Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);

       // send to all except caller client
       Clients.AllExcept(id).onNewUserConnected(id, userName);

   }

}

This is the Connect method on the server side. Notice how a client is only added if there isn't anybody else with that ConnectionId.

Sure you can send a message to someone based on their name, but at some point you will have to deal with 2 people having the same name; it is always better to have non-repeating IDs as identifiers than names.

Hope this helps! Best of luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.