Based on the wiki article here: https://github.com/SignalR/SignalR/wiki/Hubs
I can get an MVC application to broadcast messages through my hub using 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 located in a separate DLL called ServerHub.dll, looks like this:
namespace ServerHub { public class ChatterBox : Hub { public void Send(string message) { Clients.All.addMessage(message); } } }
Thus, with the above setting, I can go to the same URL in several browsers and send a message from one browser, it will be displayed in all other browsers.
However, now I am trying to send a message from the controller.
So, in my MVC home Internet application, in HomeController, "About Action", I added the following:
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 does not seem to work. No error messages or runtime error. The code is executing, I just donβt see the message in other browsers.
Where am I wrong?
source share