Reputation: 515
I need to get the group identifier and message on the function callback. Is it possible?
Here's the code:
$(function () {
// Proxy created on the fly
var chat = $.connection.discussion;
var discussionId = Math.floor(Math.random() * 2);
alert(discussionId);
var discussionId2 = Math.floor(Math.random() * 2);
alert(discussionId2);
$.connection.hub.start(function () {
chat.server.join(discussionId);
chat.server.join(discussionId2);
});
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
// Start the connection
$.connection.hub.start().done(function () {
$("#broadcast").click(function () {
// Call the chat method on the server
var discussionIdX = Math.floor(Math.random() * 2);
alert(discussionIdX);
chat.server.send(discussionIdX, $('#msg').val());
});
});
});
It's just an example. Basically I add the user to 2 random groups and then I need to I need the group identificator to append the message to the correct div.
Edited:
public class Discussion : Hub{
public void Send(string discussionId, string message)
{
Clients.Group(discussionId).addMessage(message);
}
public void Join(string discussionId)
{
Groups.Add(Context.ConnectionId, discussionId);
}}
Upvotes: 0
Views: 248
Reputation: 36073
Pass the group as a parameter from the server.
On the client:
chat.client.addMessage = function (message, group) { ... }
and on the server:
Clients.Group(discussionId).addMessage(message, discussionId);
Upvotes: 1