C# 带有 Web 套接字的 SignalR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11601561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:29:11  来源:igfitidea点击:

SignalR with Web Sockets

c#asp.netsignalr

提问by Luke Belbina

I am attempting to get websockets working in my dev environment:

我试图让 websockets 在我的开发环境中工作:

  • Visual Studio 2010
  • Windows 7
  • Signal R 0.51
  • Latest Chrome / Firefox
  • 视觉工作室 2010
  • Windows 7的
  • 信号 R 0.51
  • 最新的 Chrome / Firefox

Unfortunately the Javscript client is using long polling. When I force web-sockets on the client side I can't connect at all:

不幸的是,Javscript 客户端正在使用长轮询。当我在客户端强制使用网络套接字时,我根本无法连接:

$.connection.hub.start({ transport: ['webSockets'] })

Server code is self-hosted and based on the sample and looks like:

服务器代码是自托管的,基于示例,如下所示:

static void Main(string[] args)
{
    string url = "http://localhost:8081/";
    var server = new 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 : Hub
{            
    public void Send(string message)
    {
        Clients.addMessage(message);
    }
}

I've searched around and found nothing definitive. Do I need to specify some extra things, use Visual Studio 2012 or will this only work on Windows 8 / IIS 8?

我四处搜索,没有找到任何确定的。我是否需要指定一些额外的东西,使用 Visual Studio 2012 还是只适用于 Windows 8 / IIS 8?

采纳答案by Lasse Christiansen

Based on this answer https://stackoverflow.com/a/9135334/700926you will see that WebSocket support in SignalR relies on Windows 8 / IIS8 - the answer also points to a wiki page at SignalR's github page, however, that page does not exists anymore.

基于此答案https://stackoverflow.com/a/9135334/700926,您将看到 SignalR 中的 WebSocket 支持依赖于 Windows 8 / IIS8 - 答案还指向 SignalR 的 github 页面上的 wiki 页面,但是,该页面确实不存在了。

But, by cloning the wiki repo at github and go back some revisions you will see the documentation of the SignalR.WebSocketsproject which according to SignalR's github page, does not exist anymore - (which might explain why the wiki site is removed) - however, in a revision of the wikipage for SignalR.WebSocketsfrom February this year, it stated that:

但是,通过在 github 上克隆 wiki 存储库并返回一些修订版,您将看到该SignalR.WebSockets项目的文档,根据 SignalR 的 github 页面,该文档不再存在 - (这可能解释了为什么 wiki 站点被删除) - 然而,在SignalR.WebSockets从今年 2 月起对维基页面进行了修订,它指出:

The SignalR.WebSocketspackage can be added to an existing SignalR project to allow clients to connect using the WebSocket protocol. The SignalR jQuery client will automatically attempt to connect via WebSockets (if the browser supports it) so no changes are necessary on the client side to add WebSockets to your SignalR based application.

SignalR.WebSockets relies on Microsoft.WebSocketsin order to listen for incoming WebSocket connections from within ASP.NET. This package in turn depends on the new WebSockets support that was added to ASP.NET 4.5 and IIS 8.0. As a result, the SignalR.WebSockets package will only work on a Windows 8 machine (.NET 4.5 will install on earlier versions of Windows but Windows 8 is required for IIS 8.0). For more information on how to setup a Windows 8 machine (using the developer preview) see here.

所述SignalR.WebSockets包可以被添加到现有的SignalR项目以允许客户端使用来连接WebSocket协议。SignalR jQuery 客户端将自动尝试通过 WebSockets 进行连接(如果浏览器支持),因此客户端无需更改即可将 WebSockets 添加到基于 SignalR 的应用程序。

SignalR.WebSockets 依赖 Microsoft.WebSockets来侦听来自 ASP.NET 的传入 WebSocket 连接。该包又依赖于添加到 ASP.NET 4.5 和 IIS 8.0 的新 WebSockets 支持。因此,SignalR.WebSockets 包只能在 Windows 8 机器上运行(.NET 4.5 将安装在早期版本的 Windows 上,但 IIS 8.0 需要 Windows 8)。有关如何设置 Windows 8 计算机(使用开发人员预览版)的更多信息,请参见 此处

I have tried searching for newer information than what I have been able to provide above, but as far as I can tell, the SignalR wiki does not cover this topic explicitly in its current version.

我已经尝试搜索比我上面能够提供的信息更新的信息,但据我所知,SignalR wiki 在其当前版本中没有明确涵盖这个主题。

回答by Maarten

Even on Windows 8 / .NET 4.5 it was not working initially, but with these additional tips I finally got it working.

即使在 Windows 8 / .NET 4.5 上它最初也不起作用,但是通过这些额外的提示我终于让它工作了。

  • Install websocket support

    -> Turn Windows features on or off
    -> Internet Information Services 
    -> World Wide Web Services 
    -> Application Development Features 
    -> WebSocket Protocol
    
  • in web.config, under appSettings, add this setting:

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    
  • 安装 websocket 支持

    -> Turn Windows features on or off
    -> Internet Information Services 
    -> World Wide Web Services 
    -> Application Development Features 
    -> WebSocket Protocol
    
  • 在 web.config 中的 appSettings 下,添加以下设置:

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    

SignalR automatically negotiates websockets, it does not have to be specified and nothing special is needed in code.

SignalR 自动协商 websockets,它不必指定,代码中也不需要任何特殊内容。

回答by Leo

Signal R does work also on Windows 7, IIS 7.5 and Visual Studio 2012. The only drawback is that it will use longPolling as transport protocol.

Signal R 也适用于 Windows 7、IIS 7.5 和 Visual Studio 2012。唯一的缺点是它将使用 longPolling 作为传输协议。

In order to succesfully use signalR with the above configuration we need to set up few things, which actually are not detailed in any of the signalR tutorials.

为了成功使用具有上述配置的signalR,我们需要设置一些东西,实际上在任何signalR教程中都没有详细说明。

So, on server side (win 7, IIS 7.5) when you create the connection and the proxy you must do something like this:

因此,在服务器端(win 7,IIS 7.5)创建连接和代理时,您必须执行以下操作:

Connection = new HubConnection('someUrl', false);   
Proxy = Connection.CreateHubProxy('myHubProxy');   
Connection.Start(new LongPollingTransport()).Wait(); // notice the specification of transport

Of course, on the client side in the same way you have to specify the transport protocol as being 'longPooling':

当然,在客户端,您必须以相同的方式将传输协议指定为“longPooling”:

var connection = $.hubConnection('myUrl', { useDefaultPath: false });
connection.start({ transport: ['websockets', 'longPolling'], jsonp: true, xdomain: true })

回答by Bon

In addition to the above solutions for troubleshooting WebSockets problems in SignalR, if you are inside a corporate network (or something similarly structured) it is likely that your proxies and firewalls will interfere with the handshake.

除了上述用于对 SignalR 中的 WebSockets 问题进行故障排除的解决方案之外,如果您在公司网络(或类似结构的网络)中,您的代理和防火墙可能会干扰握手。

But, if you access it over SSL, the wrapper it provides protects it from this interference in many cases.

但是,如果您通过 SSL 访问它,它提供的包装器在许多情况下会保护它免受这种干扰。