Question

I'm trying to get list of connected users by using the following server code in SignalR hub. For store in-memory data I'm using following class:

public class UserInfo
    {
        public string ConnectionId { get; set; }
        public string UserName { get; set; }
        public string Role { get; set; }
    }

When user connected I'm adding user to the list of connected users:

public override Task OnConnected()
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    if (Context.User.IsInRole("User"))
                    {
                        ui.Add(new UserInfo { ConnectionId = Context.ConnectionId, UserName = Context.User.Identity.Name, Role = "User" });
                    }
                    else
                    {
                        ui.Add(new UserInfo { ConnectionId = Context.ConnectionId, UserName = Context.User.Identity.Name, Role = "Operator" });
                    }
                }
                return base.OnConnected();
            }

Here is the way I'm getting list of currently connected users:

public IEnumerable<UserInfo> GetUsers()
        {
            var x = (from a in ui where a.Role == "User" select new UserInfo { UserName = a.UserName, ConnectionId = a.ConnectionId }).ToList();
            return x;
        }
        public IEnumerable<UserInfo> GetOperators()
        {
            var y = (from a in ui where a.Role == "Operator" select new UserInfo { UserName = a.UserName, ConnectionId = a.ConnectionId }).ToList();
            return y;
        }

Unfortinately public method GetOperators/GetUsers not accessible and I did not receive data on client side:

$(function () {
            // Declare a proxy to reference the hub. 
            var chat = $.connection.chatHub;
            //Here I'm calling hub public methods
            chat.getOperators = function (data) {
                alert(data);
            };
            chat.getUsers = function (data) {
                alert(data);
            };
            // Create a function that the hub can call to broadcast messages.
            chat.client.addChatMessage = function (name, message) {
                // Html encode display name and message. 
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.sendChatMessage($('#displayname').val(),      $('#message').val());             
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
Was it helpful?

Solution

Your syntax for the calls to the server is wrong; this here:

chat.getUsers = function (data) {
    alert(data);
};

will simply define chat.getUsers to be a function.

You probably want

chat.server.getUsers().done(function(data) {
    console.log(data);
}).fail(function(error) {
    console.log("failed to get data", error);
});

Take another look at the documentation.

OTHER TIPS

please can you try this

 //on your client action do a server call 

 chat.server.getOperators();

and

//on your client action do a server call  
 chat.server.getUsers(); 

instead of

chat.getOperators = function (data) {
                alert(data);
            };
            chat.getUsers = function (data) {
                alert(data);
            };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top