使用相同的ConnectionId用于连接到SignalR客户端 [英] Using the same connectionId for a client connecting to SignalR

查看:854
本文介绍了使用相同的ConnectionId用于连接到SignalR客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的SignalR和我碰了壁:我现在有一台服务器(用户进来,并登录到他们的凭据的应用程序)和SignalR枢纽是上运行的ASP.NET MVC应用程序Windows服务中运行托管使用OWIN和自我托管在不同的机器上。我已经成功地让我的web应用程序和SignalR中心一起工作,客户端连接到集线器,并能够从SignalR发送/接收数据到/。

I'm rather new to SignalR and I've hit a wall: I currently have an ASP.NET MVC app running on one server (users coming in and logging into the app with their credentials) and a SignalR hub which is running inside a Windows Service hosted on a different machine using OWIN and self hosting. I've managed to get my web app and SignalR hub to work together, client is connecting to the hub and is able to send/receive data to/from SignalR.

不过,我想我的web应用程序始终连接到SignalR使用相同的ConnectionId特定用户 - 这我想preFER是用户ID(或东西在此基础上)。由于SignalR是自主办,在中心执行现有的Context对象,我没有访问从我的MVC应用程序的用户。
据我看到的,我不能从客户端(jQuery的)设置的ConnectionId,但纠正我,如果我错了......等等我怎么能达到预期的行为的任何意见或建议将是非常美联社preciated!

However, I would like my web app to always connect to SignalR for a specific user using the same connectionId - which I would prefer to be the userId (or something based on this). Since SignalR is self hosted, in the Context object available in the Hub implementation I don't have access to the User from my MVC app. As far as I saw I can't set the connectionId from the client side (jQuery), but correct me if I'm wrong... so any ideas or suggestions on how I could achieve the desired behavior would be much appreciated!

感谢您!

安德烈

推荐答案

这是不可能的配置自定义的ConnectionId的。然而,可以通过用户id,以解决客户,甚至提供自己的<一个href=\"http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections#IUserIdProvider\"相对=nofollow> IUserIdProvider 。只需使用 Clients.User(用户ID).myMethod()而不是 Clients.Client(的ConnectionId).myMethod()

It is not possible to configure custom connectionId's. It is however possible to address clients by userId and even provide your own IUserIdProvider. Simply use Clients.User(userId).myMethod() instead of Clients.Client(connectionId).myMethod().

如果你不提供自己的 IUserIdProvider ,对于给定的客户端的默认用户id将成为 Context.User.Identity的价值。名称时,表示客户端调用集线器方法。

If you don't provide your own IUserIdProvider, the default userId for a given client will be the value of Context.User.Identity.Name when said client invokes a Hub method.

这篇关于使用相同的ConnectionId用于连接到SignalR客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆