Reputation: 147
I hit the following problem. I'd like to do the following. When a new client is being connected, group parameter is being sent to the SignalR server's side (in URL or another way). Then I want to notify only clients from the specific group.
e.g.
I have 3 clients:
1) with group parameter = a
2) with group parameter = a
3) with group parameter = b
I want to notify only clients with group parameter == a. If I use dynamic field Clients, it'll send a message for all the clients. Is it possible to filter the receivers somehow?
Upvotes: 9
Views: 7813
Reputation: 146020
The syntax hfor SignalR2 is now as follows
example:
public class ContosoChatHub : Hub
{
public Task JoinRoom(string roomName)
{
return Groups.Add(Context.ConnectionId, roomName);
}
public Task LeaveRoom(string roomName)
{
return Groups.Remove(Context.ConnectionId, roomName);
}
}
Upvotes: 0
Reputation: 3952
If you want to send a message all group members, you need to add client in the group. you can define group name or you can let clients select. For example:
<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var g = $.connection.groups;
g.send = function (t) {
$("#groups").append(t);
};
$("#btnJoin").click(function () {
g.addGroup($("#gr").val());
});
$("#btnSend").click(function () {
g.sendMessage("a"); //for example a group.
});
$.connection.hub.start();
});
</script>
<select id="gr">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<div id="groups"></div>
<input id="btnJoin" type="button" value="Join"/>
<input id="btnSend" type="button" value="Send"/>
public class Groups : Hub
{
public void AddGroup(string groupName)
{
GroupManager.AddToGroup(Context.ClientId, groupName);
Clients.send(Context.ClientId + " join " + groupName + " group.<br />");
}
public void SendMessage(string groupName)
{
Clients[groupName].send(groupName + " group - Hello Everybody!");
}
}
Upvotes: 12