我试图创建一个简单的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和集线器之间的接线有问题;请有人指出我正确的方向吗?
编辑:根据JPThorne的建议更改方法签名和hub.start
看起来问题可能与代理有关;我在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
更新:
感谢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
上用于{version}的正则表达式也过早地接收了SignalR脚本!
答案 0 :(得分: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)
更新#2:
添加:
[HubName("MyHub1")]
以上public class MyHub1 : Hub
更新#3:
好的,所以我尝试了你的版本,实际上有一些问题。
hub.start();
将其称为:$.connection.hub.start();