简体   繁体   English

Signalr 2.1.1 OnConnected即使客户端回调也不触发

[英]Signalr 2.1.1 OnConnected not firing even with client callbacks

Hub code: 集线器代码:

    private readonly IHubContext<IMyHubClient> _context;

    public MyHub(IHubContext<IMyHubClient> context)
    {
      _context = context;
    }

    public override Task OnConnected()
    {
        string userName = Context.User.Identity.Name;
        string connectionId = Context.ConnectionId;

        //some code to store connected user info

        return base.OnConnected();
    }

Client code: 客户代码:

        $.connection.myHub.client.connected = function() { alert('1'); };
        $.connection.myHub.client.disconnected = function() { alert('2'); };
        $.connection.hub.start()
                  .done(function(){ console.log('Now connected, connection ID=' + $.connection.hub.id); })
                  .fail(function(){ console.log('Could not Connect!'); });

Client succesfully connects to hub and js callbacks are working. 客户端成功连接到中心,并且js回调正常工作。 But server-side OnConnected method never fires. 但是服务器端的OnConnected方法永远不会触发。 What can be the problem? 可能是什么问题?

Is MyHub getting constructed? MyHub是否正在构建? I doub't SignalR is able to supply the IHubContext<IMyHubClient> parameter. 我没有SignalR能够提供IHubContext<IMyHubClient>参数。 If you add a parameterless constructor or remove the existing one, your code should work. 如果添加无参数构造函数或删除现有的构造函数,则代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM