2017-02-08 41 views
1

我正在尝试创建一个简单的SignalR测试项目;但是,它的表现并不像我认为的那样。我有一个网站用下面的代码:信号测试项目未按预期工作

namespace SignalRTest3 
{     
    public class MyHub1 : Hub 
    { 
        public void Hello(string message) 
        { 
            Clients.All.Hello(); 
             
        } 
    } 
} 

Startup.cs:

[assembly: OwinStartupAttribute(typeof(SignalRTest3.Startup))] 
namespace SignalRTest3 
{ 
    public partial class Startup 
    { 
        public void Configuration(IAppBuilder app) 
        { 
            ConfigureAuth(app); 

      app.UseCors(CorsOptions.AllowAll); 
            app.MapSignalR(); 
        } 
    } 
} 

我已经改变了索引页:

<body> 
    <!-- HTML Content --> 

    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script type="text/javascript" src="~/Scripts/jquery.signalR-2.1.2.min.js"></script> 
    <script type="text/javascript" src="~/signalr/hubs"></script> 
    <script type="text/javascript" src="~/Scripts/SignalRTest.js"></script> 
    <div class="jumbotron"> 
        <h1>SignalR Test</h1> 
    </div> 
</body> 

而且JS文件上面提到的是:

$(function () { 
    // Declare a proxy to reference the hub.  
    var hub = $.connection.MyHub1; 

    // Create a function that the hub can call to broadcast messages. 
    hub.client.Hello = function (message) { 

        alert(message); 
    }; 

    hub.start(); 
}); 

因此,意图我当接收到通知时,该站点将显示警报。要达到这种效果,我有一个控制台应用程序:

class Program 
{ 
    static void Main(string[] args) 
    { 
        Console.Write("Message: "); 
        string message = Console.ReadLine(); 

        HubConnection connection = new HubConnection("http://localhost:4035/"); 
        IHubProxy hub = connection.CreateHubProxy("MyHub1"); 
        connection.Start().Wait(); 
        hub.Invoke<string>("Hello", message).Wait();             
    } 
} 

所以我键入消息,可以看到,在轮毂火灾的代码,但它看起来像有一些错误的JavaScript和集线器之间的连线;请有人指出我正确的方向?

编辑:修改方法签名和hub.start按JPThorne的建议

编辑:使用CORS试过,但无济于事

它看起来像这个问题可能与代理;我得到的F12控制台窗口中下面的输出:

Uncaught TypeError: Cannot read property 'MyHub1' of undefined 
    at HTMLDocument.<anonymous> (VM456 SignalRTest.js:3) 
    at c (jquery-1.10.2.min.js:21) 
    at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:21) 
    at Function.ready (jquery-1.10.2.min.js:21) 
    at HTMLDocument.q (jquery-1.10.2.min.js:21) (anonymous) @ VM456 SignalRTest.js:3 c @ jquery-1.10.2.min.js:21 fireWith @ jquery-1.10.2.min.js:21 ready @ jquery-1.10.2.min.js:21 q @ jquery-1.10.2.min.js:21 

UPDATE:

感谢来自JPThorne帮助,我终于跟踪下来的BundleConfig文件;矿山看起来像这样:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
      "~/Scripts/jquery-{version}.js")); 

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
      "~/Scripts/jquery.validate*")); 
// Use the development version of Modernizr to develop with and learn from. Then, when you're 
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need. 
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
      "~/Scripts/modernizr-*")); 

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
      "~/Scripts/bootstrap.js", 
      "~/Scripts/respond.js")); 

bundles.Add(new StyleBundle("~/Content/css").Include(
      "~/Content/bootstrap.css", 
      "~/Content/site.css")); 

,尽我所能收集,用于{}版本上~/Scripts/jquery-{version}.js正则表达式也拿起SignalR脚本太早!

+0

一个简单的问题,这两个代码都在同一个项目或两个不同的项目,因为IMO似乎是一个基于控制台的应用程序,另一个是基于Web的。它是否正确? – Prabhat

+0

是 - 单独的应用程序。用于发送消息的控制台应用程序以及用于显示该消息的Web应用程序 –

+2

您也允许CORS很重要。请参阅https://docs.microsoft.com/zh-cn/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client – Tester

回答

1

您还需要在客户端启动集线器。那么,这对我们很有用。你可以试试这个:

$.connection.hub 
    .start() 
    .done(function() { console.log("Hub started"); }) 
    .fail(function() { console.log("Error: could not connect"); }); 

另外,我发现下面的听众得心应手:

$.connection.hub.connectionSlow(function() { 
    console.log("We are currently experiencing difficulties with the connection."); 
}); 

$.connection.hub.error(function (error) { 
    console.log("SignalR error: ", error); 
}); 

更新#1: 我将您的服务器上改变Clients.All.hello();Clients.All.hello(message);

然后在FE更改hub.client.Hello = function (name, message)hub.client.Hello = function (message)

UPDATE#2 : 地址: [HubName("MyHub1")]以上public class MyHub1 : Hub

更新#3:

好了,所以我想你的版本,而且也确实存在一些问题。

  1. 不要找这样开始:hub.start();这样称呼它:$.connection.hub.start();
  2. 您可能需要升级到jQuery的最新版本。
  3. 确保你在SignalRTest.js之上引用了jquery - 这就是为什么你得到'不能读取上面未定义'属性的错误。
+0

看来,虽然这可能是正确的,但并不是完整的答案(仍然没有得到警报)。 –

+0

好了,看你的代码,再次我看到: 公共无效你好(字符串消息)和 hub.client.Hello =功能(姓名,消息){ 所以......你不使用第一个字符串消息 - 你没有通过它。在前端脚本中,您还有一个额外的名称参数。 检查我更新的答案。参数必须匹配。 – JPThorne

+0

试过了。我甚至尝试玩这个方法(Hello/hello),但没有骰子。 –