升级到SignalR 1.0.0 alpha2时,OnConnected Task不会触发

时间:2012-11-22 08:22:35

标签: c# asp.net signalr

我刚刚尝试升级到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的汇编:

enter image description here

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

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

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

2 个答案:

答案 0 :(得分:2)

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

$.connection.yourhub.client.connected = function () { };
$.connection.yourhub.client.disconnected = function () { };

答案 1 :(得分:1)

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