简体   繁体   English

来自集线器外部的SignalR广播不起作用

[英]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.

相关问题 SignalR - 从集线器外部通过另一个项目中的集线器进行广播 - SignalR - Broadcasting over a Hub in another Project from outside of a Hub SignalR集线器Authorize属性不起作用 - SignalR hub Authorize attribute doesn't work 使用GlobalHost.ConnectionManager.GetHubContext从外部集线器调用SignalR的客户端方法不起作用。但是从集线器内部呼叫确实如此 - SignalR calling client method from outside hub using GlobalHost.ConnectionManager.GetHubContext doesn't work. But calling from within the hub does 通过Hub方法触发的自定义功能进行SignalR Hub广播 - SignalR Hub broadcasting from custom function triggered by Hub method “SignalR”无法识别集线器 - “SignalR” Doesn't recognize hub 如何从外部应用程序(不使用SignalR)启动SignalR集线器 - How to fire off SignalR hub from external application (that doesn't use SignalR) Signalr 客户端没有收到集线器消息? - Signalr client doesn't get hub messages? signalR /集线器未显示集线器方法 - signalR/hubs doesn't show hub method 在没有默认构造函数的情况下返回Castle DynamicProxy对象时,SignalR hub调用不起作用 - SignalR hub invoke doesn't work when returning a Castle DynamicProxy object without a default constructor 如何从外部调用 SignalR 集线器方法? - How do I call a SignalR hub method from the outside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM