asp.net-mvc-3 – signalR维护用户连接ID
我试图维护用户的连接ID,我的意思是即使他刷新页面他得到相同的connectionid
这是我到目前为止所能做的 javascript部分 // 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); } } 然后我创建了一个像这样继承IConnectionIdFactory的类 public class MyConnectionFactory : IConnectionIdFactory { public string CreateConnectionId(IRequest request) { if (request.Cookies["srconnectionid"] != null) { return request.Cookies["srconnectionid"]; } return Guid.NewGuid().ToString(); } } 我在Application_start()中注册了上面的类,如下所示 protected void Application_Start() { AspNetHost.DependencyResolver.Register(typeof(IConnectionIdFactory),() => new MyConnectionFactory()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 我的问题是每次在CreateConnectionId中调用MyConnectionFactory类 任何人都可以建议如何解决我的问题,还是有任何其他方法来重用同一用户的连接ID …? Cookie值在客户端设置.我已经尝试了2天.这将是很好的帮助 提前致谢 解决方法
cookie的过期日期应该是UTC字符串(您没有这样做,所以很可能服务器将您的cookie视为已过期).像这样更改你的代码:
var cValue = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString()); document.cookie = cName + "=" + cValue; 或者您可以使用jQuery Cookie plugin来设置cookie. UPDATE 在您提供的代码中,cookie的名称也是不一致的.您正在设置名为“userConnectionid”的cookie,但尝试通过名称“srconnectionid”进行访问.请检查您是否在那里没有拼写错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-core – 在运行时更改注入的对象
- 为ASP.NET应用程序实现后台服务的最佳方法是什么?
- asp.net+js实现批量编码与解码的方法
- asp.net – 如何冻结GridView标题?
- asp.net-mvc – ViewModels和渲染
- asp.net-mvc – MVC模型绑定到集合不以0索引开头的集合
- asp.net-mvc – 用于Firefox的Windows认证网站提示,但不适用
- ASP.NET WebAPI中的模拟和异步
- ASP.NET GridView CommandField作为TemplateField
- asp.net-identity – 为什么一个站点的ASP.NET身份登录可以
- 如何从ASP.NET中的Web请求返回pdf?
- asp.net – 在Sitecore中检索URL路径部分的方法是
- ASP.NET hostingEnvironment / shadowCopyBinAss
- asp.net-mvc – 其中Simple Injector等效于Struc
- asp.net-core – Stream的意外结束,内容可能已被
- 是否有来自asp.net的htmlencode / htmldecode的j
- asp.net – 如何在网站项目中使用发布配置文件(.
- IIS应用程序池_缓存回收
- ASP.NET CustomErrors – RemoteOnly,其中“remo
- asp.net-mvc – ServiceStack应该是MVC应用程序中