c# - SignalR send message to specific user -


i created signalr project. want application basic whatsapp. can send message clients signalr, can't send message specific user.

here tried.

server side

public class startup {     public void configuration(iappbuilder app)     {         app.mapsignalr();     } }  public class chathub : hub {     public static string username = "";     public void send(string user, string message)     {         //it doesn't works         clients.user(username).sendmessage(message);         //it works         clients.all.sendmessage(message);     }     public override task onconnected()     {         //it works username         username = context.querystring["username"];         return base.onconnected();     } } 

client side

protected override async void onappearing() {     chathubconnection = new hubconnection("http://192.168.2.2:80/", new dictionary<string, string>{         { "username", myuser }      });     chathubproxy = chathubconnection.createhubproxy("chathub");     chathubproxy.on<string>("sendmessage", (k) => {         messages.add(string.format("msg:{0}", k));     });     await chathubconnection.start(); } private async void sendbutton_clicked(object sender, eventargs e) {     await chathubproxy.invoke<string>("send", recieverentry.text, messageentry.text); } 

use connectionid send message specific client. userid , username different things, more detail - https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections

change code following (ideally connections dictionary should extracted separate class handle username connection mapping)

public class chathub : hub {     public static concurrentdictionary<string, string> connections = new concurrentdictionary<string, string>();     public void send(string username, string message)     {         string connectiontosendmessage;         connections.trygetvalue(username, out connectiontosendmessage);          if (!string.isnullorwhitespace(connectiontosendmessage))         {             clients.client(connectiontosendmessage).sendmessage(message);         }     }     public override task onconnected()     {         if (!connections.containskey(context.connectionid))         {             connections.tryadd(context.querystring["username"], context.connectionid);         }          return base.onconnected();     } } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -