Reputation:
Quite obviously, I am screwing something up, but it seems like I've followed the documentation properly, so I'm asking for some insights.
I have built a simple ASP.NET MVC5 application, and I am testing asynchronous notification of a file state. As a proof of concept I:
I created a new NotificationHub like so:
public class Notification
{
public MessageLevels Type { get; set; }
public string Message { get; set; }
public string Title { get; set; }
}
public enum MessageLevels
{
Success,
Info,
Notice,
Error
}
public class NotificationHub : Hub
{
public void Notify(Notification model)
{
Clients.Caller.notify(model);
}
}
After this, I added the following notifications script to my Layouts page:
$(function() {
var messages = $.connection.notificationHub;
messages.client.notify = function(model) {
$.pnotify({
title: model.title,
text: model.message,
type: model.type
});
};
});
In my controller, I have something like this to test out the notification:
public ActionResult Index()
{
System.Threading.ThreadPool.QueueUserWorkItem(state => SendNotification());
return View();
}
private void SendNotification()
{
Thread.Sleep(3500);
var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All
.Notify(new Notification
{
Message = "This is a test.",
Title = "Test Message",
Type = MessageLevels.Notice
});
}
When I run this, I am monitoring the following line in my web browser:
$.pnotify({
... however, my breakpoint is never reached.
Can someone suggest what I am doing wrong?
UPDATE:
Per halter73's answer, I changed my script to this:
$(function() {
var connection = $.hubConnection();
var notificationHubProxy = connection.createHubProxy('notificationHub');
notificationHubProxy.on('notify', function(model) {
$.pnotify({
title: model.title,
text: model.message,
type: model.type
});
});
connection.start()
.done(function() { console.log('Now connected, connection ID=' + connection.id); })
.fail(function() { console.log('Could not connect'); });
});
...and the breakpoint is now being reached. However, all my vars are coming back as undefined... I'll have to play with that. I expect it has something to do with my capitalization.
Upvotes: 2
Views: 1652
Reputation: 15244
Where is your call to $.connection.hub.start()
? Is this called after you define $.connection.notificationHub.client.notify
?
If you do not define your client notify
callback before you start the connection, you will not be subscribed to the NotificationHub
. This could be why your notify
callback containing your call to $.pnotify
is never invoked.
Upvotes: 4