If you want to send a message to all members of the group, you need to add the client to the group. you can specify the name of the group, or you can let customers choose. For instance:
<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"); </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!"); } }
source share