2.3.2. ContextObject and JSON serialization

  • ContextObject

    • ConnectionId

    • Headers

    • Query string

    • Request

    • RequestCookie

    • User

  • Retrieve ContextObject

    • 1.Modify hub class

      obj.client.log = function (data) {
                        $("#logList").append("<li>----------------New connection------------</li>");
                        $("#logList").append("<li>connection id:" + data.conId + "</li>");
                        $("#logList").append("<li>connection status:" + data.conId + "</li>");
                        $("#logList").append("<li>connection transport:" + data.transport + "</li>");
                        $("#logList").append("<li>connection host:" + data.host + "</li>");
                        $("#logList").append("<li>connection port:" + data.port + "</li>");
                        $("#logList").append("<li>connection user name:" + data.uname + "</li>");
                    }
  • 2.Modified client side

    class Info
    {
          public string conId { get; set; }
          public string  conStatus { get; set; }
          public string transport { get; set; }
          public string host { get; set; }
          public string port { get; set; }
          public string uname { get; set; }
    }
    
    [HubName("myhub")]
    public class MyHub: Hub
    {
      public override Task OnConnected()
      {
          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);
      }

Last updated