Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Need to understand a signalr chatting code

Need to understand a signalr chatting code

Scheduled Pinned Locked Moved ASP.NET
comhelpquestionlounge
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    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

    D D 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      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

      D Offline
      D Offline
      deepankarbhatnagar
      wrote on last edited by
      #2

      got the ans?

      hi

      T 1 Reply Last reply
      0
      • D deepankarbhatnagar

        got the ans?

        hi

        T Offline
        T Offline
        Tridip Bhattacharjee
        wrote on last edited by
        #3

        where is the answer....? i am looking for answer.

        tbhattacharjee

        1 Reply Last reply
        0
        • T Tridip Bhattacharjee

          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

          D Offline
          D Offline
          Dar Brett
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups