Reputation: 872
I'm working on a signalR project, the project includes a Web API in the API I added my hub class then I created a separate javascript client to work with that client , through this I got the error said examhub' Hub could not be resolved. and I don't know why can any help.
My Hub Code :
public class ExamHub : Hub
{
public void Send(string name , string message)
{
Clients.All.broadcast(name, message);
}
}
Javascript Client :
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script src="~/Scripts/jquery.signalR-2.2.2.js"></script>
<script src="http://localhost:51822/signalr/hubs"></script>
<title>SignalR</title>
</head>
<body>
<div>
</div>
<script type="text/javascript">
$(function () {
var exam = $.connection.examHub;
exam.client.broadcast = function (name, message) {
alert(name + "" + message);
}
$.connection.hub.start().done(function () {
exam.server.send("Alameer", "Hi");
});
});
</script>
</body>
</html>
Upvotes: 1
Views: 1382
Reputation: 11
When you configure signalr in the api you setup the url that your client should listen too. In the below example if my api is http://localhost:51822 then the url my client will listen to would be
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR("/signalr", new HubConfiguration());
}
}
}
Then in the client project would configure it's javascript signalr connection like so
<script type="text/javascript">
$(function () {
var hub = $.connection.myHub;
$.connection.hub.url = 'http://localhost:51822/signalr';
$.connection.hub.start();
});
</script>
Keep in mind you will likely need to allow CORS in your dev environment. I would not recommend setting up your project so that CORS is allowed in production unless that is one of your requirements. Also not include in the code is the Hub that you need to setup. That information can be found here
Upvotes: 1
Reputation: 8276
You probably forgot to include the generated proxy signalr/hubs
. Try to add this line to your javascript code:
<script src="signalr/hubs"></script>
Upvotes: 0