[英]SignalR broadcasting from outside a hub doesn't work
Based on the wiki article here: https://github.com/SignalR/SignalR/wiki/Hubs 根据这里的wiki文章: https : //github.com/SignalR/SignalR/wiki/Hubs
I am able to get my MVC application to broadcast messages via my Hub with this: 我可以通过我的集线器获取我的MVC应用程序来广播消息:
$(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 我的Hub位于一个名为ServerHub.dll的独立DLL中,如下所示
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. 因此,通过上面的设置,我可以浏览几个不同浏览器上的相同URL,并从一个浏览器发送消息,将反映在所有其他浏览器中。
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: 所以在我开箱即用的MVC互联网应用程序中,在HomeController,About动作中,我添加了这个:
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? 我哪里做错了?
You're calling a method called "say" and you only have a method called "addMessage" defined on your client. 您正在调用一个名为“say”的方法,并且您只在客户端上定义了一个名为“addMessage”的方法。
Change: 更改:
context.Clients.All.say("HELLO FROM ABOUT");
To: 至:
context.Clients.All.addMessage("HELLO FROM ABOUT");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.