Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR broadcasting from outside a hub doesn't work

Based on the wiki article here: https://github.com/SignalR/SignalR/wiki/Hubs

I am able to get my MVC application to broadcast messages via my Hub with this:

$(function () {
    // Proxy created on the fly          
    var chat = $.connection.chatterBox;

    // Declare a function on the chat hub so the server can invoke it          
    chat.client.addMessage = function (message) {
        $('#messages').append('<li>' + message + '</li>');
    };

    // Start the connection
    $.connection.hub.start().done(function () {
        $("#broadcast").click(function () {
            // Call the chat method on the server
            chat.server.send($('#msg').val());
        });
    });
});

My Hub, which is in a separate DLL named ServerHub.dll, looks like this

namespace ServerHub
{
    public class ChatterBox : Hub
    {

        public void Send(string message)
        {
            Clients.All.addMessage(message);
        }
    }
}

So with the above set up, I can browse to the same URL on several different browsers, and sending a message from one browser, will be reflected across all the other browsers.

However, what i'm trying to do now is to send a message from within a controller.

So in my out-of-the-box MVC internet application, in the HomeController, About action, I added this:

using ServerHub;

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        var context = GlobalHost.ConnectionManager.GetHubContext<ChatterBox>();
        context.Clients.All.say("HELLO FROM ABOUT");

        return View();
    }

But the above doesn't seem to be working. There's no error messages, or run time error. The code executes, just that I can't see the message on my other browsers.

Where did I go wrong?

like image 308
Null Reference Avatar asked Nov 30 '12 06:11

Null Reference


1 Answers

You're calling a method called "say" and you only have a method called "addMessage" defined on your client.

Change:

    context.Clients.All.say("HELLO FROM ABOUT");

To:

    context.Clients.All.addMessage("HELLO FROM ABOUT");
like image 176
davidfowl Avatar answered Sep 22 '22 06:09

davidfowl