How can I add/remove a user from a group using the userId
Groups.Add(userid,"groupName")
that I use in my database instead of using connectionId
Groups.Add(connectionId,"groupName")
I created a mapping like here ! using the User ID Provider method and I am able to do this
Clients.Users(userId).sendMessage("asa")
but
Groups.Add(userid,"groupName")
it is not working. So how can I make work? 工作? Is there a special mapping that I don't know about or I am using this one wrong?
It is not possible to add userId
to group. You should only use connectionId
. Depending of your needs you can use one another of the approaches described in the link you provided .
For example, you can add each connection to the group named by userId
:
Groups.Add(Context.ConnectionId, userId);
And then you can send messages to the specified user:
Clients.Group(userId).sendMessage("asa");
Another use case may include determining userId
in OnConnected
method and then adding the user to needed group by its connectionId
:
var groupName = GetGroupNameByUserId(userId);
Groups.Add(Context.ConnectionId, groupName);
...
Clients.Group(groupName).sendMessage("asa");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.