I'm trying to create a simple SignalR test project; however, it isn't behaving in the way that I believe it should. I have a web site with the following code:
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();
}
}
}
I've changed the Index page:
<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>
And the JS file referenced above is:
$(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();
});
So, the intention is that, when receiving a notification, the site will display an alert. To trigger this, I have a console app:
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();
}
}
So I type a message, and can see that the code in the hub fires, but it looks like there's something wrong with the wiring between the javascript and the hub; please can someone point me in the right direction?
EDIT: change method signatures and hub.start
as per JPThorne's suggestion
EDIT: tried using CORS, but to no avail
It looks like the problem may be with the proxy; I'm getting the following output in the F12 console window:
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:
Thanks to help from JPThorne, I finally tracked it down to the BundleConfig file; mine looked like this:
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"));
As best I can gather, the regex used for {version} on ~/Scripts/jquery-{version}.js
was also picking up the SignalR scripts too early!