signalR maintaining user connection ids

孤人 提交于 2019-12-04 09:01:21

问题


I am trying to maintain a connection id for the user ,i mean even he refreshes the page he gets the same connectionid

This is what i could do till now

the javascript part

// Start the connection            

            $.connection.hub.start(function () { chat.join(projectId, userId, userName); }).done(function () {
                alert("Connected!");
                var myClientId = $.connection.hub.id;
                setCookie("srConnectionid", myClientId, 1);

            });


            function setCookie(cName, value, exdays) {

              try{

                var exdate = new Date();
                exdate.setDate(exdate.getDate() + exdays);

                var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate);
                document.cookie = cName + "=" + c_value;

              }

             catch(err){

             alert(err.Description);             

             }
       }   

and then i made a class that inherits IConnectionIdFactory like this

  public class MyConnectionFactory : IConnectionIdFactory
  {
    public string CreateConnectionId(IRequest request)
    {
        if (request.Cookies["srconnectionid"] != null)
        {
            return request.Cookies["srconnectionid"];
        }

        return Guid.NewGuid().ToString();
    }
  }

i registerd the above class in Application_start() as below

protected void Application_Start()
    {

        AspNetHost.DependencyResolver.Register(typeof(IConnectionIdFactory), () => new MyConnectionFactory());     
        AreaRegistration.RegisterAllAreas(); 
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

My problem is everytime MyConnectionFactory class is called inside the CreateConnectionId request.Cookies["srconnectionid"] is null everytime,so the user is assigned new connectionid everytime.I could find only one link that helped me to maintain connection ids.It is http://www.kevgriffin.com/maintaining-signalr-connectionids-across-page-instances

Can any one suggest how to fix my problem or is there any other approach to reuse the connection id for the same user...?

The Cookie value is set in the clientside.I have been trying this for 2 days.It would be great help

Thanks in advance


回答1:


The expire date for the cookie should be UTC string (you are not doing this, so most probably the server is treating your cookie as expired). Change your code like this:

var cValue = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = cName + "=" + cValue;

Or you might just use jQuery Cookie plugin for setting the cookie.

UPDATE

Also the name of cookie is inconsitent in the code you have provided. You are setting the cookie with name 'userConnectionid' but trying to access by name 'srconnectionid'. Please check if you haven't made a spelling error there.




回答2:


Make sure you set the cookie's path, especially if the page is sitting in a subdirectory. I'm following a similar approach, and when the cookie path is set to root, then it all works.



来源:https://stackoverflow.com/questions/9518394/signalr-maintaining-user-connection-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!