如何设置C#winforms应用程序来托管SignalR Hubs?

我已阅读SignalR文档并观看了一些video,但我无法让SignalR在winforms应用程序中托管。

我尝试过在SignalR wiki上使用源代码: https : //github.com/SignalR/SignalR/wiki/Self-host

如果你看一下“Full Sample – Hubs”,那么“server”变量是什么? 我不明白这是如何工作的或如何将其转换为C#。 根据维基“默认的SelfHost实现是基于HttpListener构建的,可以托管在任何类型的应用程序中(控制台,Windows服务等)。”

我想在C#中托管SignalR并在asp.net中使用它。 有谁可以请我解释一下这个问题?

Wiki中的示例工作正常。

请使用NuGet(程序包管理器控制台)安装SignalR.Hosting.Self软件包

安装包SignalR.Hosting.Self

Server位于SignalR.Hosting.Self命名空间中。

样品

控制台应用

 using System; namespace MyConsoleApplication { static class Program { static void Main(string[] args) { string url = "http://localhost:8081/"; var server = new SignalR.Hosting.Self.Server(url); // Map the default hub url (/signalr) server.MapHubs(); // Start the server server.Start(); Console.WriteLine("Server running on {0}", url); // Keep going until somebody hits 'x' while (true) { ConsoleKeyInfo ki = Console.ReadKey(true); if (ki.Key == ConsoleKey.X) { break; } } } public class MyHub : SignalR.Hubs.Hub { public void Send(string message) { Clients.addMessage(message); } } } } 

Asp.NET / Javascript

     

如果您有其他答案,请发表评论

为了使其适用于C#和ASP.NET,我不得不使用“跨域”。

在我使用的JavaScript中:

  

并补充说:

 $.connection.hub.url = 'http://localhost:8081/signalr'