简体   繁体   English

如何从外部调用 SignalR 集线器方法?

[英]How do I call a SignalR hub method from the outside?

This is my Hub code:这是我的Hub代码:

public class Pusher : Hub, IPusher
{
    readonly IHubContext _hubContext = GlobalHost.ConnectionManager.GetHubContext<Pusher>();

    public virtual Task PushToOtherInGroup(dynamic group, dynamic data)
    {
        return _hubContext.Clients.Group(group).GetData(data);
    }
}

I want call this method in another project with this code:我想用这段代码在另一个项目中调用这个方法:

var pusher = new Pusher.Pusher();
pusher.PushToOtherInGroup("Test", new {exchangeTypeId, price});

I want call PushToOtherInGroup ,when calling the method i don't get any error.but pusher does not work.我想调用PushToOtherInGroup ,调用该方法时我没有收到任何错误。但是推送器不起作用。

This is my Ui Code:这是我的用户界面代码:

$(function() {
    hub = $.connection.pusher;
    $.connection.hub.start()
        .done(function() {
            hub.server.subscribe('newPrice');
            console.log('Now connected, connection ID=' + $.connection.hub.id);
        })
        .fail(function() { console.log('Could not Connect!'); });
});

(function() {
    hub.client.GetData = function (data) {
        debugger;
    };
});

What is my problem?我的问题是什么?

You can't instantiate and call a hub class directly like that. 您无法像这样直接实例化和调用集线器类。 There is much plumbing provided around a Hub class by the SignalR runtime that you are bypassing by using it as a "plain-old class" like that. SignalR运行时在Hub类周围提供了许多管道,您可以通过将其用作类似的“普通类”来绕过它。

The only way to interact with a SignalR hub from the outside is to actually get an instance of an IHubContext that represents the hub from the SignalR runtime. 与来自外部的SignalR中心进行交互的唯一方法是实际得到的实例IHubContext ,它代表了SignalR运行的枢纽。 You can only do this from within the same process, so as long as your other "project" is going to be running in process with the SignalR code it will work. 您只能在同一个过程中执行此操作,因此只要您的其他“项目”将使用SignalR代码运行,它就可以运行。

If your other project is going to be running in another process then what you would want to do is expose a sort of "companion" API which is either another SignalR hub or a regular old web service (with ASP.NET web API) that you can call from this other application to trigger the behavior you want. 如果你的其他项目将在另一个进程中运行,那么你想要做的是暴露一种“伴侣”API,它是另一个SignalR中心或常规的旧Web服务(使用ASP.NET Web API)可以从其他应用程序调用来触发您想要的行为。 Whichever technology you choose, you would probably want to secure this so that only your authenticated applications can call it. 无论您选择哪种技术,您都可能希望确保这一点,以便只有经过身份验证的应用程序才能调用它。

Once you decide which approach you're going to take, all you would do to send messages out via the Pusher hub would be: 一旦您决定采用哪种方法,通过Pusher集线器发送消息的所有操作都将是:

// Get the context for the Pusher hub
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<Pusher>();

// Notify clients in the group
hubContext.Clients.Group(group).GetData(data);

If you're looking to call a method in your hub from another project then it needs to reside within the same app domain. 如果您希望从另一个项目中调用集线器中的方法,则需要将其驻留在同一个应用程序域中。 If it does here's how you can do it: 如果它确实如此,你可以这样做:

Call a hub method from a controller's action (don't mind the title, it works for your scenario) 从控制器的动作调用hub方法 (不介意标题,它适用于您的场景)

Take a look at this link at the topic of (How to call client methods and manage groups from outside the Hub class). 请查看主题的链接(如何从Hub类外部调用客户端方法和管理组)。
Code example simply creates a singleton instance of the caller class and pass in the IHubContext into it's constructor. 代码示例只是创建调用者类的单例实例,并将IHubContext传递给它的构造函数。 Then you have access to desired context.Clients in caller class's methods: 然后你可以访问所需的context.Clients调用者类的方法:

// This sample only shows code related to getting and using the SignalR context.
public class StockTicker
{
    // Singleton instance
    private readonly static Lazy<StockTicker> _instance = new     Lazy<StockTicker>(() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>()));

private IHubContext _context;

private StockTicker(IHubContext context)
{
    _context = context;
}

// This method is invoked by a Timer object.
private void UpdateStockPrices(object state)
{
    foreach (var stock in _stocks.Values)
    {
        if (TryUpdateStockPrice(stock))
        {
            _context.Clients.All.updateStockPrice(stock);
        }
    }
}

The methods within Hub are supposed to be called FROM a CLIENT. Hub 中的方法应该从客户端调用。 If you want to send something TO a CLIENT - indeed, you have to use hubContext .如果你想向客户发送一些东西 - 实际上,你必须使用hubContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 signalR调用服务器方法,该方法调用集线器外部的回调方法。 如何从该方法调用客户端功能? - signalR calls a server method and that method calls a callback method ,outside the hub. How can I call client function from that method? SignalR - 如何从服务器上调用服务器上的Hub方法 - SignalR - How can I call Hub Method on server from server 从存储库项目调用信号器集线器方法 - Call signalr hub method from repository project 从控制器调用 SignalR Core Hub 方法 - Call SignalR Core Hub method from Controller SignalR如何在1个集线器中调用2种不同的方法? - SignalR how to call 2 different method in 1 Hub? 如何从客户端调用的SignalR Hub方法中获取Nancy ISession实例? - How do I get a Nancy ISession instance within a SignalR Hub method called from the client? 如何从SignalR集线器接收主题消息? - How do I receive Topic messages from a SignalR hub? 从SignalR中的客户端调用Hub类之外的服务器方法 - Calling a server method that is outside of the Hub class from the client in SignalR SignalR:如何真正从服务器/C# 调用集线器的方法 - SignalR: How to truly call a hub's method from the server / C# SignalR - 从集线器外部通过另一个项目中的集线器进行广播 - SignalR - Broadcasting over a Hub in another Project from outside of a Hub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM