服务器到客户端的消息不SignalR经历在ASP.NET MVC 4 [英] Server to client messages not going through with SignalR in ASP.NET MVC 4

查看:95
本文介绍了服务器到客户端的消息不SignalR经历在ASP.NET MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的测试应用程序来复制我在我的主要的应用程序有问题。

I created a simple test app to reproduce a problem I'm having in my main app.

我有以下枢纽类:

[HubName("testHub")]
public class TestHub : Hub
{
    public TestHub()
    {
        System.Diagnostics.Debug.WriteLine("TestHub instantiated");
    }

    public void RunMe()
    {
        System.Diagnostics.Debug.WriteLine("Client Started");
    }

    public static void Notify(string msg)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
        hubContext.Clients.All.notify("Hello!");
    }
}

我的测试网页是:

My test web page is:

<form action="javascript: void(0)" method="post">
    <input type="button" value="Do It!" onclick="hitHub()"/>
</form>

<div id="error"></div>
<script type="text/javascript">
    var tHub;    
    $(document).ready(function () {
        tHub = $.connection.testHub;
        tHub.notify = function (msg) {
            alert(msg);
        }
        $.connection.hub.start().done(function () {
            tHub.server.runMe();
        });
    });

    function hitHub() {
        $.ajax({
            type: "POST",
            url: "@Url.Content("~/Hub/Test")" ,
            success: function (data, textStatus, jqXHR) {
            },
            error: function (data, textStatus, jqXHR) {
                $("#error")[0].innerHTML = data.responseText;
                alert("Error notifying hub.");
            }
        });

    }
</script>

最后,我HubController:

And finally, my HubController:

public class HubController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public void Test()
    {
        TestHub.Notify("Got it!");
    }
}

在我的Application_Start,我叫RouteTable.Routes.MapHubs();

In my Application_Start, I call RouteTable.Routes.MapHubs();

该中心被实例化。然后邵仁枚()的调用被传递到服务器。这一切工作正常。

The hub gets instantiated. Then the call to runMe() gets passed to the server. This all works fine.

在哪里失败是当我点击做吧!按钮。 hitHub()被调用和我HubController.Test()方法被调用。 TestHub.Notify(知道了!)没有任何错误得到执行,但是什么也没有发生在客户端上。

Where it fails is when I click on the "Do It!" button. hitHub() gets called and my HubController.Test() method gets called. TestHub.Notify("Got it!") gets executed without any errors, however nothing happens on the client.

我错过了什么?

更新1:基于回答JcFx,改变的JavaScript的上方,使得tHub.notify设置调用$ .connection.hub.start()之前。然而,问题仍然存在。

Update 1: Based on answer from JcFx, changed the javascript above so that tHub.notify is set before calling $.connection.hub.start(). The problem remains, however.

更新2:
什么小提琴手看到:

Update 2: What fiddler sees:

更新3::当我跟踪到了MessageBus.Publish()调用,我注意到塔的主题没有预订的,所以话题永远不会被调度。我不知道如何在什么时候我应该检查要做出的subscriptiont ...

Update 3: When I trace into the MessageBus.Publish() call, I notice tha the Topic has no subscriptions, so the topic never gets scheduled. I'm not sure how at what point I should be checking for the subscriptiont to be made...

推荐答案

您使用了错误的语法:

var tHub;    
$(document).ready(function () {
    tHub = $.connection.testHub;
    tHub.client.notify = function (msg) {
        alert(msg);
    }
    $.connection.hub.start().done(function () {
        tHub.server.runMe();
    });
});

注意在轴套上的 .client 属性注册回调。

这篇关于服务器到客户端的消息不SignalR经历在ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆