Problems With SignalR...Can't get friends list to load.

bigmike2238

Member
Joined
Dec 24, 2013
Messages
7
Programming Experience
Beginner
Hello,

I have spent all day on this issue, and haven't gotten very far. I am new to SignalR, and therefore, I apologize for any glaring mistakes that might be part of this post.

Here is the code I utilized from asp.net:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Max.Hubs
{
    public class ConnectionMapping<T>
    {
        //Creates a dictionary where T is the user name and the HashSet is the connection ID
        private readonly Dictionary<T, HashSet<string>> _connections =
            new Dictionary<T, HashSet<string>>();

        //Counts the number of connections
        public int Count
        {
            get
            {
                return _connections.Count;
            }
        }

        //Method to add a connection based on user name and connection ID
        public void Add(T key, string connectionId)
        {
            lock (_connections)
            {
                HashSet<string> connections;
                if (!_connections.TryGetValue(key, out connections))
                {
                    connections = new HashSet<string>();
                    _connections.Add(key, connections);
                }

                lock (connections)
                {
                    connections.Add(connectionId);
                }
            }
        }

        //Gets connections based on username
        public IEnumerable<string> GetConnections(T key)
        {
            HashSet<string> connections;
            if (_connections.TryGetValue(key, out connections))
            {
                return connections;
            }

            return Enumerable.Empty<string>();
        }

        //Removes connections...
        public void Remove(T key, string connectionId)
        {
            lock (_connections)
            {
                HashSet<string> connections;
                if (!_connections.TryGetValue(key, out connections))
                {
                    return;
                }

                lock (connections)
                {
                    connections.Remove(connectionId);

                    if (connections.Count == 0)
                    {
                        _connections.Remove(key);
                    }
                }
            }
        }
    }
}

I will explain what I was trying to do on the server side...

whenever a user connects to the page via javasript, a instance of that user's friends list is built and a check is made to see if that user is in it, if so the user should be added to the friend's list.

C#:
namespace Max.Hubs
{
    [Authorize]
    public class FriendsListHub : Hub
    {
         public void SendChatMessage (string who, string message)
        {
            string name = Context.User.Identity.Name;

            Clients.Group(who).addChatMessage(name + ": " + message);
        }

        public override Task OnConnected()
        {
            Friendsdb _friends = new Friendsdb();
            //gets ID of current user
            var userId = HttpContext.Current.User.Identity.GetUserId().ToString();
            //Populates friendslist (lambda)
            var friends = _friends.FriendsList
                    .OrderBy(f => f.RequesteeID)
                    .Where(f => f.RequesterID == userId).ToList()
                    .Select(f => new FriendsList
                    {
                        id = f.id,
                        UserName = f.UserName   
                    });

            var username = HttpContext.Current.User.Identity.Name.ToString();
            foreach (var item in friends)
            {
                if (username == item.UserName.ToString())
                {
                    Groups.Add(Context.ConnectionId, username);
                    return base.OnConnected();
                }
                return base.OnConnected();
            }
            return base.OnConnected();
        }
    }

}

This is seemingly called by the client using...

C#:
@if (User.Identity.IsAuthenticated)
    {
    <script>
       
        $(function () {
            var friendslist = $.connection.FriendsListHub;

            // Create a function that the hub can call back to display users.
            friendslist.client.addChatMessage = function (who, message) {
                $('#display').append('<li><strong>' + htmlEncode(who)
                   + '</strong>: ' + htmlEncode(message) + '</li>');
            };

            $.connection.hub.start().done(function () {
                $('#sendmessage2').click(function () {
                    friendslist.server.SendChatMessage($('#users').val(), $('#users').val());
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

I tried everything to get this to work, and I am out of ideas...I even tried using a button so that the friendslist hub would be called but no luck. Once again I am very new to SignalR and Javascript, so please excuse my ignorance of both topics. Thanks to all that help.

-Mike
 
Back
Top Bottom