简体   繁体   English

如何设置一个C#winforms应用程序来托管SignalR集线器?

[英]how to setup a C# winforms application to host SignalR Hubs?

I have read through the SignalR docs and watched a few of the videos, however I can not get SignalR to host within a winforms application. 我已经阅读了SignalR文档并观看了一些视频,但是无法将SignalR托管在winforms应用程序中。

I have tried using source code off the SignalR wiki: https://github.com/SignalR/SignalR/wiki/Self-host 我已经尝试过使用SignalR Wiki上的源代码: https : //github.com/SignalR/SignalR/wiki/Self-host

If you look at the "Full Sample - Hubs", what is the "server" variable? 如果查看“完整样本-集线器”,则“服务器”变量是什么? I do not understand how this works or how to convert it to C#. 我不明白这是如何工作的,或者如何将其转换为C#。 According to the wiki "The default SelfHost implementation is built on HttpListener and can be hosted in any kind of application (Console, Windows Service etc). " 根据Wiki“默认的SelfHost实现基于HttpListener构建,并且可以托管在任何类型的应用程序(控制台,Windows Service等)中。”

I would like to host SignalR in C# and consume it in asp.net. 我想在C#中托管SignalR,并在asp.net中使用它。 Could anyone please shed some light on this for me? 有人可以帮我阐明一下吗?

The sample in the Wiki works fine. Wiki中的示例工作正常。

Please install the SignalR.Hosting.Self package using NuGet (Package Manager Console) 请使用NuGet(Package Manager Console)安装SignalR.Hosting.Self软件包

Install-Package SignalR.Hosting.Self 安装包SignalR.Hosting.Self

The Server lives in the SignalR.Hosting.Self namespace. Server位于SignalR.Hosting.Self命名空间中。

Sample 样品

Console Application 控制台应用

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 ASP.NET / Javascript

<script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
<script src="/Scripts/jquery.signalR.js" type="text/javascript"></script>
<script src="http://localhost:8081/signalr"></script>

<script type="text/javascript">
    $(function () {
       // create signalr hub connection
       myHub= $.connection.myHub;

       // start hub connection and call the send method
       $.connection.hub.start(function () {
           myHub.Send('Hello');
       });
    });
</script>

Please leave a comment if you have additional answers 如果您还有其他答案,请发表评论

In order to make this to work for C# and ASP.NET I had to use "Cross Domain". 为了使它适用于C#和ASP.NET,我必须使用“跨域”。

In the JavaScript I used: 在JavaScript中,我使用了:

<script type="text/javascript" src='http://localhost:8081/signalr/hubs'></script>

and added: 并添加:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM