4

我最近刚刚尝试升级到 signalR 1.0.0 Alpha2。我浏览了 wiki,更改了我认为需要更改的内容,并确保程序集都加载了新文件。这是我的js:

$(function () {
    var Chat = $.connection.ChatHub;
    ...
    $.connection.hub.start(
    .done(function(){ alert("Now connected!"); })
    .fail(function(){ alert("Could not Connect!"); });
});

这是我的中心类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;
using MyProject.Domain.Abstract;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Web.Security;
using MyProject.Domain.Entities;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
using MyProject.Domain.Concrete;
using MyProject.Domain.Helpers;
using System.Web.Mvc;

namespace MyProject.Web.RealTime
{
    public class ChatMessage
    {
        public string ChatId { get; set; }
        public string Message { get; set; }
        public string FullName { get; set; }
        public string Time { get; set; }
        public string School { get; set; }
        public string Major { get; set; }
        public string ImageUrl { get; set; }
    }
   [HubName("ChatHub")]
    public class Chat : Hub
    {
        //Initialize Repoisotries 
        private IProfileRepository profileRepository;
        private ISiteDataRepository siteDataRepository;
        private IChatRepository chatRepository;
        private IOnlineUserRepository onlineUserRepository;
        private IRecentActivityRepository recentActivityRepository;


        MembershipUser MainUser = Membership.GetUser();
        UserProfile MainProfile;
        //In memory objects
        //public static Dictionary<Guid, List<string>> onlineUsers = new Dictionary<Guid, List<string>>();


        //Constructor Injection for profile repository and site data repository
        public Chat(IProfileRepository repo, ISiteDataRepository siteRepo, IChatRepository chatRepo, IOnlineUserRepository onlineUserRepo, IRecentActivityRepository recentActivityRepo)
        {            
            profileRepository = repo;
            siteDataRepository = siteRepo;
            chatRepository = chatRepo;
            onlineUserRepository = onlineUserRepo;
            MainProfile = profileRepository.GetProfile((Guid)MainUser.ProviderUserKey);
            recentActivityRepository = recentActivityRepo;
        }



        public override Task OnDisconnected()
        {

            disconnectUser();
            List<ContactItem> contacts = profileRepository.GetFollowedOnlineContacts(MainProfile.UserId);
            foreach (ContactItem ci in contacts)
            {
                Clients.Group(Convert.ToString(ci.ChatId)).onlineUserHandler(Convert.ToString(MainProfile.ChatId), false);
            }
            return base.OnDisconnected();
        }
        public override Task OnReconnected()
        {
            connectUser();
            List<ContactItem> contacts = profileRepository.GetFollowedOnlineContacts(MainProfile.UserId);
            foreach (ContactItem ci in contacts)
            {
                Clients.Group(Convert.ToString(ci.ChatId)).onlineUserHandler(Convert.ToString(ci.ChatId), true);
            }
            return base.OnReconnected();
        }
        public override Task OnConnected()
        {
            Debug.WriteLine("Connected``````````````````````````````````");
            connectUser();
            List<ContactItem> contacts = profileRepository.GetFollowedOnlineContacts(MainProfile.UserId);
            foreach (ContactItem ci in contacts)
            {
                Clients.Group(Convert.ToString(ci.ChatId)).onlineUserHandler(Convert.ToString(MainProfile.ChatId), true);
            }
            return base.OnConnected();
        }
        private void connectUser()
        {
            if (onlineUserRepository.ConnectUser(MainProfile.UserId, Context.ConnectionId))
            {
                Groups.Add(Context.ConnectionId, Convert.ToString(MainProfile.ChatId));
            }
        }
        private void disconnectUser()
        {
            onlineUserRepository.DisconnectUser(MainProfile.UserId, Context.ConnectionId);
            Groups.Remove(Context.ConnectionId, Convert.ToString(MainProfile.ChatId));
        }





    }
}

在客户端,我收到警报说我已连接,但是当我运行调试并在“OnConnected”任务中放置一个断点时,它永远不会在那里中断或在我的集线器类中运行任何东西。我做错了什么?

这是我的新 SignalR 程序集:

在此处输入图像描述

我曾经在 App_Start 文件夹中的 NinjectWebCommon.cs 文件中使用它,但是自从我升级后它给了我错误,所以我注释掉了,也可能是问题,但不确定因为 signalR.ninject 似乎没有工作或给出我一堆错误:

GlobalHost.DependencyResolver =  new NinjectDependencyResolver(kernel);
        GlobalHost.DependencyResolver.Register(typeof(IConnectionIdGenerator), () => new MyConnectionFactory());

    RouteTable.Routes.MapHubs(new NinjectDependencyResolver(kernel));
4

2 回答 2

2

您应该在开始对方法的集线器引用之前添加 JavaScript:

$.connection.yourhub.client.connected = function () { };
$.connection.yourhub.client.disconnected = function () { };
于 2013-03-11T20:39:37.863 回答
1

问题是因为我使用的是 NinjectDependancyResolver 的未更新版本,我不确定他们是否更新了它。事实上,我已经停止在我的集线器类中使用依赖注入,因为我无法让它工作。

于 2013-04-22T17:42:33.357 回答