English 中文(简体)
信号呼唤 系统中其他地方的中枢客户
原标题:Calling SignalR hub clients from elsewhere in system

我建立了信号监测中心,供服务器和客户之间沟通。 中心服务器侧代码储存在一个称为Hooking.c的类别中。 我想要的是,能够采用Hooking所定义的方法。 c 允许我在申请中向来自任何地方的任何相关客户广播信息。 似乎出现了新的 H。 c 是为每个客户/服务商发出的呼吁而设立的,因此,我希望我能够使用类似的东西。

var hooking = new Hooking();
hooking.Test();

采用Hooking界定的方法测试。 c 诸如

public static void Test() {
    Clients.test()
}

A. 与客户一方的保险单

var hooking = $.connection.hooking;
hooking.test = function() { alert("test worked"); };
$.connection.hub.start()

不幸的是,由于客户并非静止不变,因此无法通过静态方法获得这种简单信息。

通过《信号源代码一》,我采用了一种方法,探讨有希望的,即<代码>/ .Invoke (string centralName, string methods, params Object[] args),因此我希望我能够使用诸如 Hubs.Invoke(Hooking),“试验”等内容,但我可以做这项工作。

任何帮助都将受到极大赞赏。

问题回答

这是信号R 2.x的正确方式:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.addMessage(message);

Basically, you can use the dependency resolver for the current host to resolve the IConnectionManager interface which allows you to get ahold of the context object for a hub.

详情见 正式文件

Hub.GetClients在0.4.0版中消失。

https://github.com/SignalR/SignalR/wiki//2007/5。 现在可以使用:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>();

您可以方便地使用hub,遵循这2步。

  1. 证明这种依赖性注射

    public class ClassName
    {
        ........
        ........
        private IHubContext _hub;
    
        public BulletinSenderController(IConnectionManager connectionManager)
        {
            _hub = connectionManager.GetHubContext<McpHub>();
            ........
            ........
        }
    
        ............
        ............
    }
    

2. 更改<代码>hub 象这样的物体

_hub.Clients.All.onBulletinSent(bulletinToSend);

详情见

Example code can be found in this git repo.

这一点在NET Core 2中有所改变,现在你可以像现在这样使用依赖注射:

    private readonly IHubContext<MyHub,IMyHubInterface> _hubContext;

    public MyController(MyHub,IMyHubInterface hubContext)
    {
        _hubContext = hubContext;
    }

    public bool SendViaSignalR()
    {
        _hubContext.Clients.All.MyClientSideSignalRMethod(new MyModel());
        return true;
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签