Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  4] [ 7]  / answers: 1 / hits: 36522  / 11 Years ago, wed, june 26, 2013, 12:00:00

I am new to using SignalR (started today), Pretty simple to send a message to ALL clients connected, but now I want to just send to a group. I cannot find simple documentation on how to join on the client side. If anyone can help, how can I SIMPLY join a group on the javascript side. Thanks for any help.



public class EventHub : Hub
{
public void SendNewMedia(MediaInfoViewModel model,Guid eventId)
{
Clients.Group(eventId.ToString()).setupmedia(model);
}
}
//Controller that is sending client new data
var eventHub = GlobalHost.ConnectionManager.GetHubContext<EventHub>();
var result = eventHub.Clients.Group(eventId.ToString()).setupmedia(eventViewer);

//Finally the javascript. Not sure how to setup just for a group
$(function () {
var event = $.connection.eventHub;
event.client.setupmedia = function (newMedia) {

$('#photolist').prepend('<li><img src=' + newMedia.MediaUrl + ' class=img-polaroid span2/></li>');
};
$.connection.hub.start(function() {
event.server.create(eventID);//I know this is wrong but not sure how to connect
}).done(function () {
alert('conntected. Ready to retrieve data!');
});
});

More From » c#

 Answers
40

You can't. If you could join a group from javascript then anyone may use your code to join any group which breaks security. If you really need to do that - create a method on the server side that takes a group name as parameter and adds the client to the group.



public void JoinGroup(string groupName)
{
this.Groups.Add(this.Context.ConnectionId, groupName);
}


Afterwards, call it from JS like that



eventHub.server.joinGroup(my-awsm-group);

[#77412] Monday, June 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;