SignalR问题...可以'不能加载好友列表。

第2238章

成员
已加入
2013年12月24日
留言内容
7
编程经验
Beginner
你好,

我花了整整一整天的时间在这个问题上,而且距离还很远。我是SignalR的新手,因此,对于本文中可能出现的任何明显错误,我深表歉意。

这是我从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);
                    }
                }
            }
        }
    }
}

我将解释我在服务器端尝试执行的操作...

每当用户通过javasript连接到该页面时,都会构建该用户的好友列表的实例,并检查该用户是否在其中,如果是,则应将该用户添加到好友列表中。

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();
        }
    }

}

客户端似乎使用了...

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>

我尽一切努力使它正常工作,但我没什么主意...我什至尝试使用按钮,这样就可以调用Friendslist Hub,但没有运气。我再次对SignalR和Javascript还是陌生的,所以请原谅我对这两个主题的无知。感谢所有这些帮助。

-麦克风
 
最佳 底部