简体   繁体   English

如何使用ASP.NET Hosting在Nancy中使用SignalR将服务器与客户端通信?

[英]How to communicate Server to Client with SignalR in Nancy with ASP.NET Hosting?

Most of the examples I've found for SignalR are assuming ASP.NET (MVC or not). 我为SignalR找到的大多数示例都是假设使用ASP.NET(是否使用MVC)。 I'm using NancyFX . 我正在使用NancyFX I'm having just one problem, so I'm hoping there's something I'm overlooking or some thing I need to do in Nancy to compensate for not being ASP.NET. 我只遇到一个问题,所以我希望在Nancy中有一些我要忽略的事情,或者我需要做一些事情来弥补不是ASP.NET的不足。

My one goal is to be able to notify the client browsers when a server event happens. 我的一个目标是能够在服务器事件发生时通知客户端浏览器。 I don't plan on replacing my Nancy routes with hub methods. 我不打算用集线器方法替换我的南希路线。 But I would like the ability to call into the browser from my routes (actions). 但是我希望能够从我的路线(动作)中调用浏览器。

I have very simple Hub that I created following the example in the SignalR Wiki . 我有一个非常简单的Hub,它是根据SignalR Wiki中的示例创建的。 I'm not even sure I need it, since I don't plan on calling client to server. 我什至不确定我是否需要它,因为我不打算将客户端调用到服务器。

public interface IUserNotifier
{
    void Start();
    void Notify(object @event);
}

I used an interface in hopes that I would be able to inject the same hub later on to use in my nancy routes... I'm not sure that is in the cards. 我使用了一个接口,希望以后可以注入相同的集线器以在我的南希路由中使用...我不确定这是不是在卡中。

[HubName("userNotifier")]
public class UserNotifier : Hub, IUserNotifier
{        
    public void Start()
    {
        Notify(new {Status = "Started"});
    }

    public void Notify(object @event)
    {
        Clients.notification(@event);
    }
}

When I have the following code in my html file, I can see that it executes the Start() method, and then the Notify() method, delivering content to my client. 当我的html文件中包含以下代码时,我可以看到它执行Start()方法,然后执行Notify()方法,将内容传递给客户端。

var communicator = $.connection.userNotifier;

$.extend(communicator, {
    Notification: function(event) {
        alert("notification received from server!");
        console.log(event);
    }
});

$.connection.hub.start()
    .done(function() {
        communicator.start();
});

Like I said, "starting" the hub works and sends a notification to the client. 就像我说的那样,“启动”集线器可以工作并将通知发送给客户端。 Very cool. 很酷。 But, then, my primary goal hasn't been accomplished yet. 但是,我的主要目标尚未实现。 I need to initiate these notifications from other places in my code where they might not be directly associated with a "request". 我需要从代码中可能未与“请求”直接关联的其他地方启动这些通知。

I tried injecting my IUserNotifier in my nancy modules for use in the routes, but when the Notify() is fired, I get: 我尝试将我的IUserNotifier注入我的nancy模块中以用于路由,但是当Notify()被触发时,我得到:

无法对空引用执行运行时绑定

That's because the Clients property on the Hub base class is null (hasn't been initialized). 这是因为Hub基类上的Clients属性为null(尚未初始化)。 So, I switched gears. 所以,我换了档。 I tried to follow multiple examples, including the example from the wiki page about hubs in the section called "Broadcasting over a Hub from outside of a Hub": 我尝试遵循多个示例,包括Wiki页面上有关集线器的示例,该节在“从集线器外部通过集线器广播”部分中:

public class NotifierModule : NancyModule
{
    public NotifierModule(){

        Get["/notify/{message}"] = p => { 
            var context = GlobalHost.ConnectionManager.GetHubContext<UserNotifier>();
            context.Clients.notification(new { Message = p.message });
        };
    }
}

My Nancy route executes without throwing errors. 我的南希路线执行时不会抛出错误。 Except my browser never receives the message. 除非我的浏览器从未收到此消息。 If I set a breakpoint in the route, I can see that Clients is initialized. 如果在路由中设置断点,则可以看到Clients已初始化。 Maybe the collection of clients is initialized but empty. 客户端集合可能已初始化但为空。 Who knows? 谁知道? Maybe you do. 也许你会。 :) :)

Again, my main goal is to be able to send events/notifications to the browser from anywhere in my code, any time. 同样,我的主要目标是能够随时随地从代码中的任何位置向浏览器发送事件/通知。 Is that too much to ask? 那是过分的要求? What should I be doing here? 我在这里应该做什么?

I'm sure you must have found the answer already. 我确定您一定已经找到答案。 However, I figured I could try and help out in case anyone else runs into a similar problem. 但是,我认为我可以尽力帮助其他人遇到类似问题。 In order for your server on the .NET side to send messages to clients, it would also need to have a connection made to the hub. 为了使您在.NET端的服务器向客户端发送消息,它还需要与集线器建立连接。

var connection = new HubConnection("http://localhost/");
connection.Start();
connection.Notify("Hello");

Check out an official example at: 在以下位置查看官方示例:

https://github.com/SignalR/SignalR/blob/master/samples/Microsoft.AspNet.SignalR.Client.Samples/Program.cs https://github.com/SignalR/SignalR/blob/master/samples/Microsoft.AspNet.SignalR.Client.Samples/Program.cs

暂无
暂无

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

相关问题 从 SignalR 服务器(Asp.Net Core)调用特定客户端 - Call specific client from SignalR server (Asp.Net Core) 与asp.net中的SignalR建立唯一的服务器-客户端连接 - Establish a unique server-client connection with SignalR in asp.net 如何在服务器端捕获 ASP.NET Core 2 SignalR 异常并在客户端使用 JavaScript 处理它们? - How to catch ASP.NET Core 2 SignalR exceptions on server-side and handle them on client side with JavaScript? 在 ASP.NET Core SignalR 中,如何从服务器向客户端发送消息? - In ASP.NET Core SignalR, how do I send a message from the server to a client? 检测到对ASP.NET SignalR服务器的连接尝试。此客户端仅支持连接到ASP.NET Core SignalR服务器 - Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server 如何在ASP.NET中使用SignalR从客户端计算机获取信息? - How to obtain information from the client computer with SignalR, in asp.net? 使用IIS服务器上的ASP.NET托管的SignalR Hub的403(禁止)响应 - 403 (Forbidden) response from SignalR Hub using ASP.NET hosting on IIS server SignalR在ASP.NET和控制台应用程序之间通信 - SignalR Communicate between asp.net and Console Application SignalR-客户端需要ASP.net吗? - SignalR - ASP.net required for the client? 如何使ASP.NET网站与TCP服务器应用程序通信 - How to make a ASP.NET website communicate with TCP server application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM