是什么导致SignalR在连接时收到Net::ERR_CONNECTION_RESET? [英] What causes SignalR to receive net::ERR_CONNECTION_RESET on connect?
问题描述
我有一个已添加到旧版ASP.NET MVC应用程序的简单单数示例。
以下是各个部分:
OWIN启动类
[assembly: OwinStartup(typeof (Startup))]
namespace MyApp.Web
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
SignalR集线器
using System.Collections.Generic;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace MyApp.Web
{
[HubName("podService")]
public class PodServiceHub : Hub
{
public PodServiceHub()
{
;
}
public IEnumerable<string> GetMessages()
{
return new[] {"blah", "blah", "blah"};
}
}
}
服务器端外观
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace MyApp.Web
{
public class PodService
{
PodService(IHubConnectionContext<dynamic> clients)
{
Clients = clients;
}
public PodService()
: this(GlobalHost.ConnectionManager.GetHubContext<PodServiceHub>().Clients)
{
}
IHubConnectionContext<dynamic> Clients { get; set; }
public void SendMessageToClient(string message)
{
Clients.All.doSomething(message);
}
}
}
启动Javascript的部分内容:
var podService = $.connection.podService;
...
$.extend(podService.client, {
doSomething: function(message) {
console.log("received message:" + message);
}
});
// test
$.connection.hub.start()
.done(function() {
podService.server.getMessages()
.done(function(messages) {
console.log("received message:" + message);
});
});
在第一页调用的一个控制器中:
_podService.SendMessageToClient("Hello from the server!");
执行应用程序时,Chrome的dev工具控制台显示以下错误:
到‘ws://localhost:62025/signalr/connect?transport=webSockets&;clientProtocol=1.5&;connectionToken=02LJFqBcRBWKXAOlaSwgMPWG0epV7AFl19gNjFCvA0dxD2QH8%2BC9V028Ehu8fYAFN%2FthPv65JZKfK2MgCEdihCJ0A2dMyENOcdPkhDzEwNB2WQ1X4QXe1fiZAyMbkZ1b&;connectionData=%5B%7B%22name%22%3A%22podservice%22%7D%5D&;tid=6’的WebSocket连接失败:WebSocket握手期间出错:NET::ERR_CONNECTION_RESET
但是,在此错误之后,podService.server.getMessages()返回消息,将消息从服务器打印["blah","blah","blah"]到控制台,随后调用DoSomething客户端函数,打印"Received Message:Hello from the server!"。
来自客户端和服务器的调用都在传输数据,因此此错误似乎不会中断应用程序。不过,这看起来肯定是个问题。上面的代码基于Microsoft.AspNet.SignalR.Sample NuGet包生成的示例代码,该包没有显示相同的行为。我知道我的示例和基于NuGet的示例之间唯一的不同之处在于,我将它添加到了一个遗留的MVC应用程序中,而不是一个纯基于owin的应用程序。根据我读到的评论on this SO question,这应该不是问题。那么,此示例用法有什么问题和/或可能导致连接重置的原因?
推荐答案
我确保:
- the web socket component is enabled in IIS
- and the "allowKeepAlive" setting is not set to false in my web.config
但在我的案例中,问题是由软件包版本不匹配引起的:
在Packages.config中,Microsoft.Owin.Host.SystemWeb版本2.1.0被意外引用
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net452" />
当所有其他OWIN相关包都引用版本3.0.1时
我已将Microsoft.Owin.Host.SystemWeb更新到版本3.0.1
然后我必须确保在web.config文件中"httpRuntime"元素针对的是框架版本4.5
<system.web>
<httpRuntime targetFramework="4.5" />
</system.web>
以上两个步骤解决了问题。
这篇关于是什么导致SignalR在连接时收到Net::ERR_CONNECTION_RESET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!