Need to understand a signalr chatting code
-
i was reading a signalr private & public chat code and it looks great but few area i just do not understand. i was reading from this url http://www.tugberkugurlu.com/archive/mapping-asp-net-signalr-connections-to-real-application-users https://github.com/tugberkugurlu/SignalRSamples/blob/master/ConnectionMappingSample/ConnectionMappingSample/Hubs/ChatHub.cs public class User { public string Name { get; set; } public HashSet ConnectionIds { get; set; } } public class ChatHub : Hub { private static readonly ConcurrentDictionary Users = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); public override Task OnConnected() { string userName = Context.User.Identity.Name; string connectionId = Context.ConnectionId; var user = Users.GetOrAdd(userName, _ => new User { Name = userName, ConnectionIds = new HashSet() }); lock (user.ConnectionIds) { user.ConnectionIds.Add(connectionId); // // broadcast this to all clients other than the caller // Clients.AllExcept(user.ConnectionIds.ToArray()).userConnected(userName); // Or you might want to only broadcast this info if this // is the first connection of the user if (user.ConnectionIds.Count == 1) { Clients.Others.userConnected(userName); } } return base.OnConnected(); } } i do not understand OnConnected() function very well. so please help me to understand it. why they use HashSet class ? if some one see the full code from my given url then please tell me how they could handle users with same name but different connection id? thanks
tbhattacharjee
-
i was reading a signalr private & public chat code and it looks great but few area i just do not understand. i was reading from this url http://www.tugberkugurlu.com/archive/mapping-asp-net-signalr-connections-to-real-application-users https://github.com/tugberkugurlu/SignalRSamples/blob/master/ConnectionMappingSample/ConnectionMappingSample/Hubs/ChatHub.cs public class User { public string Name { get; set; } public HashSet ConnectionIds { get; set; } } public class ChatHub : Hub { private static readonly ConcurrentDictionary Users = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); public override Task OnConnected() { string userName = Context.User.Identity.Name; string connectionId = Context.ConnectionId; var user = Users.GetOrAdd(userName, _ => new User { Name = userName, ConnectionIds = new HashSet() }); lock (user.ConnectionIds) { user.ConnectionIds.Add(connectionId); // // broadcast this to all clients other than the caller // Clients.AllExcept(user.ConnectionIds.ToArray()).userConnected(userName); // Or you might want to only broadcast this info if this // is the first connection of the user if (user.ConnectionIds.Count == 1) { Clients.Others.userConnected(userName); } } return base.OnConnected(); } } i do not understand OnConnected() function very well. so please help me to understand it. why they use HashSet class ? if some one see the full code from my given url then please tell me how they could handle users with same name but different connection id? thanks
tbhattacharjee
got the ans?
hi
-
got the ans?
hi
where is the answer....? i am looking for answer.
tbhattacharjee
-
i was reading a signalr private & public chat code and it looks great but few area i just do not understand. i was reading from this url http://www.tugberkugurlu.com/archive/mapping-asp-net-signalr-connections-to-real-application-users https://github.com/tugberkugurlu/SignalRSamples/blob/master/ConnectionMappingSample/ConnectionMappingSample/Hubs/ChatHub.cs public class User { public string Name { get; set; } public HashSet ConnectionIds { get; set; } } public class ChatHub : Hub { private static readonly ConcurrentDictionary Users = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); public override Task OnConnected() { string userName = Context.User.Identity.Name; string connectionId = Context.ConnectionId; var user = Users.GetOrAdd(userName, _ => new User { Name = userName, ConnectionIds = new HashSet() }); lock (user.ConnectionIds) { user.ConnectionIds.Add(connectionId); // // broadcast this to all clients other than the caller // Clients.AllExcept(user.ConnectionIds.ToArray()).userConnected(userName); // Or you might want to only broadcast this info if this // is the first connection of the user if (user.ConnectionIds.Count == 1) { Clients.Others.userConnected(userName); } } return base.OnConnected(); } } i do not understand OnConnected() function very well. so please help me to understand it. why they use HashSet class ? if some one see the full code from my given url then please tell me how they could handle users with same name but different connection id? thanks
tbhattacharjee
I think that if a user has multiple tabs or windows open in their web browser then each one will be considered a different web socket connection. A HashSet isn't something I've used, but I believe it is a high performance List that will never store duplicate values. I would recommend reading the [MSDN remarks]((which I believe is essentially the same as a List<T>)) on the class to make sure you understand what it is: The reason for this HashSet would be to ensure that all connections to a given user are associated with each other, so that a message intended for that user would be sent to all of his active connections. I think the idea is that your user name would be a unique value - for example a user logging into your site using their email address. If you wanted to make users share the same visible user name, you would still need to give them a unique identifier to track them with. I understand that English is probably not your primary language, but is there some specific point in that tutorial link that you sent that you're having difficulty understanding? I'm afraid that without you being more specific I would only be able to repeat whatever is written in that article, and probably only confuse you more.