2.3.3. Clients Object's Methods

  • Pass message to specific client

  • Modify hub:

    • 1.Create a list to store connection ID

      private static List<string> ConnectionIds;
      
        public MyHub()
        {
            ConnectionIds = new List<string>();
        }
    • 2.Add/Remove message on connected/disconnected

      public override Task OnConnected()
        {
            ConnectionIds.Add(Context.ConnectionId);
      
            Info i = new Info();
            i.conId = Context.ConnectionId.ToString();
            i.conStatus = Context.Headers["Connection"].ToString();
            i.transport = Context.QueryString["transport"];
            i.host = Context.Request.Url.Host;
            i.port = Context.Request.Url.Port.ToString();
            i.uname = Context.User.Identity.Name;
      
            return Clients.All.log(i);
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            ConnectionIds.Remove(Context.ConnectionId);
            return Clients.All.log("Disconnected" + DateTime.Now.ToString());
        }
    • 3.Send message to specific one

      Clients.Client(ConnectionIds[1]).clientmethod(name, ConId, msg);
    • 4.Broadcast message except specific one

      Clients.AllExcept(ConnectionIds[1]).clientmethod(name, ConId, msg);

Last updated