0

我在 ASP.NET 4.5 Web 窗体应用程序中使用 SignalR。该应用程序位于 IIS 8.0 中,托管在http://www.smarterasp.net/上,它支持 ASP.NET 4.5 Web Sockets。

SignalR 在协商后尝试连接到 Web 套接字端点:

SignalR: Connecting to websocket endpoint 'ws://XXXXXXXX.smarterasp.net/signalr/connect?transport=webSockets&connectionToken=ixkMLqEJ4QZBQKxY_Gzbn3J82nrTTA9wy2WhcEDm1mRN8x8i1pY1Z-imcFQ0wy5Yi0KnaFcl0z8BmnXn1K5aPwiaXsSghfzAXOv5pHJD53mT9O_0mVi1YSzNWC2jNFNI0&connectionData=%5B%7B%22name%22%3A%22xxxxxxxxhub%22%7D%5D&tid=9' 

但它失败了。浏览器 (Chrome 29) 显示在控制台上:

WebSocket connection to 'ws://XXXXXXXX.smarterasp.net/signalr/connect?transport=webSockets&connectionToken=ixkMLqEJ4QZBQKxY_Gzbn3J82nrTTA9wy2WhcEDm1mRN8x8i1pY1Z-imcFQ0wy5Yi0KnaFcl0z8BmnXn1K5aPwiaXsSghfzAXOv5pHJD53mT9O_0mVi1YSzNWC2jNFNI0&connectionData=%5B%7B%22name%22%3A%22xxxxxxxxhub%22%7D%5D&tid=9' failed: Unexpected response code: 500 

我通过在服务器事件日志中搜索来调查这个 500 错误,我发现了这个异常:

Exception type: InvalidOperationException

Exception message: Not a valid web socket request.
   at Microsoft.AspNet.SignalR.Owin.ServerRequest.AcceptWebSocketRequest(Func`2 callback)
   at Microsoft.AspNet.SignalR.Transports.WebSocketTransport.ProcessRequest(ITransportConnection connection)
   at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(HostContext context)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(HostContext context)
   at Microsoft.AspNet.SignalR.Owin.CallHandler.Invoke(IDictionary`2 environment)
   at Microsoft.AspNet.SignalR.Owin.Handlers.HubDispatcherHandler.Invoke(IDictionary`2 environment)
   at Microsoft.Owin.Host.SystemWeb.OwinCallContext.Execute()
   at Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object extraData)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result)
   at Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

SignalR 成功地正确回退到 ServerSentEvents trasnport,但是如果客户端和服务器都支持它们,我想使用 Web 套接字。

这些是 Chrome 开发栏中请求的选项卡标题信息:

Request URL:ws://XXXXXXXX.smarterasp.net/signalr/connect?transport=webSockets&connectionToken=ixkMLqEJ4QZBQKxY_Gzbn3J82nrTTA9wy2WhcEDm1mRN8x8i1pY1Z-imcFQ0wy5Yi0KnaFcl0z8BmnXn1K5aPwiaXsSghfzAXOv5pHJD53mT9O_0mVi1YSzNWC2jNFNI0&connectionData=%5B%7B%22name%22%3A%22xxxxxxxxhub%22%7D%5D&tid=9


Request Header

Cache-Control:no-cache
Connection:Upgrade
Cookie:ASPSESSIONIDSARSTSCR=KGFDPPHBMLAOPCJOIJMJCGHA; __utma=175391648.1719372995.1378025058.1380629683.1380719917.14; __utmc=175391648; __utmz=175391648.1380629683.13.3.utmcsr=member.smarterasp.net|utmccn=(referral)|utmcmd=referral|utmcct=/account/loginform; ksucookie=EAAAAHjizGN3Q+766OL12jm34an9xl7a7bDSRrzNaVMW17OF
Host:XXXXXXXX.smarterasp.net
Origin:http://XXXXXXXX.smarterasp.net
Pragma:no-cache
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:dgGEfrCFCUmN9CFG2QzvbA==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)         Chrome/29.0.1547.76 Safari/537.36


Query String Parameters

transport:webSockets
connectionToken:ixkMLqEJ4QZBQKxY_Gzbn3J82nrTTA9wy2WhcEDm1mRN8x8i1pY1Z-imcFQ0wy5Yi0KnaFcl0z8BmnXn1K5aPwiaXsSghfzAXOv5pHJD53mT9O_0mVi1YSzNWC2jNFNI0
connectionData:[{"name":"xxxxxxxxhub"}]
tid:9

这个web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <customErrors mode="Off" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"></validation>
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="ApiURIs-ISAPI-Integrated-4.0" path="people/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />  
    </handlers>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    ...
  </connectionStrings>
  <appSettings>
    ...
  </appSettings>
</configuration>`

我在SignalR Web Sockets IIS8 Server 2012 - "Not a web socket request"上找到了 stackoverflow 的答案,所以我去http://www.websocket.org/echo.html测试了一个 web socket connection: only connections"wss: //”(使用安全的 WebSocket (TLS))工作,而不是正常的“ws://”连接不起作用。

有什么建议么?

4

0 回答 0