Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 信号器2.2客户端未接收任何消息_Javascript_C#_Signalr - Fatal编程技术网

Javascript 信号器2.2客户端未接收任何消息

Javascript 信号器2.2客户端未接收任何消息,javascript,c#,signalr,Javascript,C#,Signalr,我有一个在控制台应用程序上下文中运行的自托管Signal应用程序。我通过使用包装器类连接到其中的集线器,以避免必须引用ASP.NET项目中的signar.Core程序集。因此,它是我的应用程序中的另一个C#类,负责向连接的客户端广播消息 我可以直接从Javascript调用PlanHub方法(Register和Unregister),这些断点会被命中。但是,当我从中心之外的类调用客户机方法时(即使我使用Clients.All来消除组注册问题),客户机永远不会收到消息。我做错了什么 运行此代码时,

我有一个在控制台应用程序上下文中运行的自托管Signal应用程序。我通过使用包装器类连接到其中的集线器,以避免必须引用ASP.NET项目中的signar.Core程序集。因此,它是我的应用程序中的另一个C#类,负责向连接的客户端广播消息

我可以直接从Javascript调用
PlanHub
方法(
Register
Unregister
),这些断点会被命中。但是,当我从中心之外的类调用客户机方法时(即使我使用
Clients.All
来消除组注册问题),客户机永远不会收到消息。我做错了什么

运行此代码时,我可以验证
Clients.All.updateStatus(planId,message)成功命中代码,但客户端控制台上没有记录任何内容。

以下是相关代码:

PlanHub.cs

public class PlanHub : Hub
{
    private const string GroupPrefix = "PlanGroup_";

    public void Register(int companyId)
    {
        Groups.Add(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }

    public void Unregister(int companyId)
    {
        Groups.Remove(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }
}
public class PlanPublisher
{
    private readonly static Lazy<PlanPublisher> _instance = new Lazy<PlanPublisher>(() => 
        new PlanPublisher(GlobalHost.ConnectionManager.GetHubContext<PlanHub>().Clients));
    private IHubConnectionContext<dynamic> Clients { get; set; }

    private PlanPublisher(IHubConnectionContext<dynamic> clients)
    {
        Clients = clients;
    }

    public static PlanPublisher Instance => _instance.Value;

    public void UpdateStatus(int planId, string message)
    {
        //This code gets hit and no exceptions are thrown, but the clients
        //never receive the message. Using Clients.All instead of my 
        //Groups for testing -- still doesn't work
        Clients.All.updateStatus(planId, message);
    }
}
PlanPublisher.Instance.UpdateStatus(plan.Id, $"Publishing started for {plan.Name}...");
PlanPublisher.cs

public class PlanHub : Hub
{
    private const string GroupPrefix = "PlanGroup_";

    public void Register(int companyId)
    {
        Groups.Add(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }

    public void Unregister(int companyId)
    {
        Groups.Remove(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }
}
public class PlanPublisher
{
    private readonly static Lazy<PlanPublisher> _instance = new Lazy<PlanPublisher>(() => 
        new PlanPublisher(GlobalHost.ConnectionManager.GetHubContext<PlanHub>().Clients));
    private IHubConnectionContext<dynamic> Clients { get; set; }

    private PlanPublisher(IHubConnectionContext<dynamic> clients)
    {
        Clients = clients;
    }

    public static PlanPublisher Instance => _instance.Value;

    public void UpdateStatus(int planId, string message)
    {
        //This code gets hit and no exceptions are thrown, but the clients
        //never receive the message. Using Clients.All instead of my 
        //Groups for testing -- still doesn't work
        Clients.All.updateStatus(planId, message);
    }
}
PlanPublisher.Instance.UpdateStatus(plan.Id, $"Publishing started for {plan.Name}...");
Javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
<script src="~/scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:8080/signalr/hubs"></script>
<script>
    $(document).ready(function() {
        $.connection.hub.url = "http://localhost:8080/signalr";

        var planMessagePublisher = $.connection.planHub;

        planMessagePublisher.client.updateStatus = function (planId, message) {
            console.log('Status: ' + planId + ' - ' + message);
        };

        $.connection.hub.start().done(function() {
            //This works! My PlanHub.Register method gets called successfully
            planMessagePublisher.server.register(@Model.CompanyId);

            // New connection ID is successfully sent to console
            console.log('Now connected, connection ID=' + $.connection.hub.id);
        });
    });
</script>
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Auto detected cross domain url.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Client subscribed to hub 'planhub'.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Negotiating with 'http://localhost:8080/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22planhub%22%7D%5D'.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: webSockets transport starting.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Connecting to websocket endpoint 'ws://localhost:8080/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAA5D8YUVyzBEG4tTlTaGn0MgAAAAACAAAAAAADZgAAwAAAABAAAABDjF1MaCTzZI0XTM8gC29xAAAAAASAAACgAAAAEAAAAKrk0jv%2FKF4YFzDvNwmSR8IoAAAAacm1d1r7dJpjOtVtCFIFRpugkubyZm1e5Z8OtOFtnhZyEBO1SO4lqhQAAABiG7hBydTiypPh8k2ZYz20ropNxw%3D%3D&connectionData=%5B%7B%22name%22%3A%22planhub%22%7D%5D&tid=6'.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Websocket opened.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: webSockets transport connected. Initiating start request.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: The start request succeeded. Transitioning to the connected state.
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Invoking planhub.Register
jquery.signalR-2.2.0.min.js?v=8588299:8 [06:23:19 GMT-0700 (Pacific Daylight Time)] SignalR: Invoked planhub.Register
它成功注册,但从未收到任何数据。
Clients.All.updateStatus(planId,message)断点被多次命中,但控制台从未收到任何其他日志记录数据

编辑:有人建议我研究一下自定义依赖项解析程序是否在使用中。这里发布的代码位于它自己的项目中,除了您在这里看到的内容之外,几乎没有其他内容。调用
PlanPublisher.UpdateStatus()
的代码确实有一个自定义依赖项解析程序,但这并不重要,因为它在自己的程序集中是独立的
PlanPublisher.cs
PlanHub.cs
包含在一个非常简单的项目中,该项目只引用了signer.Core和signer.SelfHost。

尝试以下方法:

public void UpdateStatus(int planId, string message)
{
    var clients = GlobalHost.ConnectionManager.GetHubContext<PlanHub>().Clients;
    clients.All.updateStatus(planId, message);
}
public void UpdateStatus(int-planId,字符串消息)
{
var clients=GlobalHost.ConnectionManager.GetHubContext().clients;
clients.All.updateStatus(planId,message);
}

我认为问题在于PlanPublisher是在创建集线器上下文(和客户端)之前初始化的。

我试图简化您的示例(使用SignalR 2.2.0)

枢纽等级:

public class PlanHub : Hub
{
    private const string GroupPrefix = "PlanGroup_";

    // hubcontext
    private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <PlanHub> ();

    public void Register(int companyId)
    {
        Groups.Add(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }

    public void Unregister(int companyId)
    {
        Groups.Remove(Context.ConnectionId, $"{GroupPrefix}{companyId}");
    }

    // static method using hub context
    public static void Static_UpdateStatus(int planId, string message)
    {
        hubContext.Clients.All.updateStatus(planId, message);
    }
}
JS:

浏览器控制台中的结果:


谢谢你的建议。只是尝试了一下,但行为还是一样的。你能发布.js控制台调试吗<代码>$.connection.hub.logging=true您需要提供再现问题的最小示例。我只是试着在服务器端和客户端都使用您提供的代码运行,结果运行得很好,没有任何问题。@Scott,您设法解决了吗?因为我面临着同样的问题。有趣的是,如果我试着为团队发表文章,它是有效的,但不是所有人都有效。另一方面,当我创建一个单独的项目来尝试这个功能时,一切都很好。