问题描述
我可以添加例如用户名代替的 connection.id 的。它的工作原理?我问这是因为过度的ConnectionId我不能发送通知给用户,因为总的ConnectionId是变化?
can i add e.g. username instead of connection.id. does it work? i'm asking this because over ConnectionId i can't send notification to the user, because ConnectionId always is changes?
Groups.Add(my_username,MyGroup1的);
Groups.Add("my_username", "mygroup1);
推荐答案
没有你不行,你必须使用的ConnectionId。但是,您可以做到以下几点。
No you cannot, you must use a ConnectionID. You can however do the following.
在集线器商店的用户列表映射到连接ID:
On your hub store a list of users mapped to connection IDs:
static ConcurrentDictionary<string, User> _users = new ConcurrentDictionary<string, User>();
然后,客户端连接启动后,你可以调用的方法,如:
Then after a client connection has been started you can call into a method like:
myHub.server.createUser("MyUsername");
当然,在服务器上你必须:
Of course on the server you'd have:
public void createUser(string userName)
{
// You'd create a User class that had both of the following properties
var user = new User
{
UserName = userName,
ConnectionID = Context.ConnectionId
};
_users.TryAdd(user.ConnectionID, user);
}
所以每当你想通过查找连接ID的用户,你仍然有他/她通过_users词典的用户名的情况下。
SO whenever you want to lookup a user via connection ID you still have context of his/her username via the _users dictionary.
注:此code通过signalr工程1.0阿尔法+
Note: This code works via signalr 1.0 alpha +
这篇关于signalr组添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!