SingalR没有调用服务器端方法并将数据广播回客户端

时间:2016-06-16 19:51:13

标签: javascript asp.net owin

我有一个正常运行的工作,注册到OWIN主机应用程序,我把stock ticker broadcasting app的项目作为参考,我想做的工作就像股票代码类能够广播数据到JavaScript客户端。

然而,在我按照这个例子后,把一切都搞定了,发生了一些奇怪的问题,我无法弄清楚原因。 (1)流量将自动降级为长轮询,而不是像股票代码那样使用服务器发送事件。 (2)我在服务器端调用了一个动态方法,并在客户端定义了该方法,但它从未调用过

这是我的服务器端集线器定义

[HubName("healthCheckHub")]

public class HealthCheckHub : Hub
{

   private readonly PublicVendorCloudHealthJob _healthCheckjob;

    public HealthCheckHub()
    {
        this._healthCheckjob = PublicVendorCloudHealthJob.Instance;
    }

    public IEnumerable<HealthCheckItemResponse> GetAllHealthCheckResponses()
    {
        return this._healthCheckjob.GetAllHealthCheckResponses();
    }
}

这是我的Job类实现

public class PublicVendorCloudHealthJob : SkyJobGrain, IPublicVendorCloudHealthJob
{
    private readonly ConcurrentBag<HealthCheckItemResponse> _response;
    private static IConfiguration _configuration;
    private static IDeploymentElevator _deploymentElevator;
    private static IItineraryElevator _itineraryElevator;
    private static IResourceElevator _resourceElevator;

    public PublicVendorCloudHealthJob(IConfiguration configuration, IDeploymentElevator deploymentElevator, IItineraryElevator itineraryElevator, IResourceElevator resourceElevator)
        : base(configuration, deploymentElevator, itineraryElevator, resourceElevator)
    {
        this.Clients = GlobalHost.ConnectionManager.GetHubContext<HealthCheckHub>().Clients;
        _configuration = configuration;
        _deploymentElevator = deploymentElevator;
        _itineraryElevator = itineraryElevator;
        _resourceElevator = resourceElevator;
        this._response = new ConcurrentBag<HealthCheckItemResponse>
        {
            new HealthCheckItemResponse
            {
                Description = "Larissa test"
            }
        };
    }

    public override Task Execute(object obj)
    {
        this.Clients.All.publishHealthChecks("Larissa" + DateTime.UtcNow);
        return TaskDone.Done;
    }

    public static PublicVendorCloudHealthJob Instance => (PublicVendorCloudHealthJob)Activator.CreateInstance(typeof(PublicVendorCloudHealthJob), _configuration, _deploymentElevator, _itineraryElevator, _resourceElevator);

    public IEnumerable<HealthCheckItemResponse> GetAllHealthCheckResponses()
    {
        return this._response;
    }
    private IHubConnectionContext<dynamic> Clients { get; }
}

我还在Owin startup.cs文件中配置集线器,如下所示

        app.UseCors(CorsOptions.AllowAll);
        GlobalHost.Configuration.KeepAlive = null;
        GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromSeconds(5);
        app.MapSignalR();

对于JS客户端,在我获得生成的代理后,我确实像这样

signalrService.signalr().then((value) => {
    if (value === "success"){
        const healthCheckHub = $.connection.healthCheckHub;
        const healthCheckHub2 = $.connection.hub.proxies.healthcheckhub;
        healthCheckHub.client.publishHealthChecks = (data) => {
            console.log(data);
        };
        healthCheckHub2.client.publishHealthChecks = (data) => {
            console.log(data);
        };
        $.connection.hub.logging = true;
        $.connection.hub.start().done(() => {
            const defaultData = healthCheckHub.server.getAllHealthCheckResponses();
            console.log(defaultData);
        });
    }
});

真的需要一些帮助,我已经花了大约一个星期来解决这个问题,谢谢先进

P.S。 我使用的东西如下所示 (1)Chrome v.51 (2)SingalR.core v.2.2.0 (3)Owin

0 个答案:

没有答案