ASP.NET Web API Signalr没有找到OnConnected方法

时间:2018-09-10 10:20:38

标签: c# jquery signalr azure-service-fabric signalr.client

我在本地Service Fabric群集上托管的ASP.NET有状态项目内部有一个集线器,并且我覆盖了 OnConnected 方法。在 OnConnected 方法上设置断点时,不会输入该断点,但是同时,消息会通过集线器正确广播。

补习班:

public class NotificationHub : BaseHub
{
    private IFragStateManager _stateManager;
    private static IHubContext _context;

    public NotificationHub(IFragStateManager stateManager) : this(stateManager, _context)
    {
        _stateManager = stateManager;
        _context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
    }

    private NotificationHub(IFragStateManager stateManager, IHubContext context) : base(stateManager, context)
    { }

    /// <summary>
    /// Broadcasts payload to all clients
    /// </summary>
    /// <param name="payload"></param>
    public void Broadcast(object payload)
    {
        try
        {
            _context.Clients.All.broadcast(payload);
        }
        catch (Exception e)
        {
            Log.Error(e.ToString());
        }            
    }

    //Investigate more why it's not hitting breakpoint while it actually connects and broadcasts messages
    public override Task OnConnected()
    {
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        return base.OnDisconnected(stopCalled);
    }
}

_context.Clients.All.broadcast(有效载荷); 效果很好。成功交付有效载荷

集线器的基类:

    public abstract class BaseHub : Hub
    {
        protected IPrincipal User {
            get { return base.Context.User ?? _user; }
        }

        private ClaimsPrincipal _user;
        private Guid _userId;

        private IFragStateManager _stateManager;
        private IHubContext _context;

        public BaseHub(IFragStateManager stateManager, IHubContext context)
        {
            _stateManager = stateManager;
            _context = context;
        }        

        public override async Task OnConnected()
        {
            GetUserId();

            var map = await _stateManager.GetOrAddAsync<FragDictionary<Guid, string>>("UserConnectionMap");

            using (var tx = _stateManager.CreateTransaction())
            {
                var exisingValue = await map.TryGetValueAsync(tx, _userId);
                await map.SetAsync(tx, _userId, Context.ConnectionId);
                await tx.CommitAsync();
            }

            await base.OnConnected();
        }        

        public override async Task OnDisconnected(bool stopCalled)
        {
            var map = await _stateManager.GetOrAddAsync<FragDictionary<Guid, string>>("UserConnectionMap");

            using (var tx = _stateManager.CreateTransaction())
            {
                await map.TryRemoveAsync(tx, _userId); 
                await tx.CommitAsync();
            }

            await base.OnDisconnected(stopCalled);
        }

        private void GetUserId()
        {

            if (Context.User == null)
                _user = (ClaimsPrincipal)(IPrincipal)Context.Request.Environment["server.User"];
            else
                _userId = new Guid(_user.FindFirst("AccountId").Value);
        }
    }

集线器客户端:

    $.connection.hub.logging = true;
    $.connection.hub.url = 'http://localhost:8298/api/realtime/70c604f8-7996-4107-9ec3-28d08c869899/131810464642593740/4b98412e-8f09-4460-9c90-1d155cb8efca/signalr';

    var hub = $.connection.notificationHub;

    $.extend(hub.client, {
        broadcast: function (msg) {console.log(msg)}
    });

    $.connection.hub.start().done(function() {
        console.log('Connected');
    });   

客户的脚本:

<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.js"></script>
<script src="http://localhost:8298/api/realtime/70c604f8-7996-4107-9ec3-28d08c869899/131810464642593740/4b98412e-8f09-4460-9c90-1d155cb8efca/signalr/hubs"></script>

已检查的内容:

  1. 降级的SignalR。从2.3降至2.1。没有结果。
  2. js客户端已经在订阅,因此这里可能没有问题。
  3. 从后端代码中拨出Hub时,它将广播消息 正确。

0 个答案:

没有答案