如何从外部调用SignalR集线器方法? [英] How do I call a SignalR hub method from the outside?

查看:113
本文介绍了如何从外部调用SignalR集线器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 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});

我想调用 PushToOtherInGroup ,在调用该方法时我没有收到任何错误.但是pusher不起作用.

I want call PushToOtherInGroup,when calling the method i don't get any error.but pusher does not work.

这是我的用户界面代码:

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;
    };
});

我怎么了?

推荐答案

您不能像这样直接实例化和调用集线器类.SignalR运行时在Hub类周围提供了许多管道,您可以通过将其用作普通类"来绕过它.

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集线器进行交互的唯一方法是从SignalR运行时实际获取代表该集线器的 IHubContext 的实例.您只能在同一过程中执行此操作,因此只要您的其他项目"将与SignalR代码一起在过程中运行,它将可以正常工作.

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. 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.

如果您的其他项目将要在另一个进程中运行,那么您想要做的就是公开一种伴侣" API,它可以是另一个SignalR集线器或常规的旧Web服务(带有ASP.NET Web API)),您可以从该其他应用程序中调用以触发所需的行为.无论选择哪种技术,您都可能希望对此进行保护,以便只有经过身份验证的应用程序才能调用它.

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. Whichever technology you choose, you would probably want to secure this so that only your authenticated applications can call it.

一旦您决定采用哪种方法,通过Pusher集线器发送消息所要做的一切就是:

Once you decide which approach you're going to take, all you would do to send messages out via the Pusher hub would be:

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

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

这篇关于如何从外部调用SignalR集线器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆