我正在运行一个非常简单的SignalR服务器,通过Owin在Ubuntu Server 14.04上进行自托管,使用的是Mono 3.2.8。(下方为代码)
连接/断开连接在远程Windows服务器以及将程序部署到Linux服务器时都能正常工作。但是当客户端意外死机而没有通知SignalR他已经断开连接时,在Linux服务器上会出现永无止境的SocketException异常,而在Windows服务器上则是大约30秒后就会断开客户端连接。但是在Linux服务器上,这个socketexception (如下)每隔10秒左右就会不停地报错。
我该如何让Linux服务器在运行相同的代码时像Windows服务器一样,设定一段超时时间后断开用户连接,并且不抛出socketexceptions?
服务器代码:
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Owin;
namespace signalrtestserver
{
class Program
{
static void Main(string[] args)
{
var uri = System.Configuration.ConfigurationManager.AppSettings["startup_uri"] ?? "http://*:7890";
using (Microsoft.Owin.Hosting.WebApp.Start<Startup>(uri))
{
Console.WriteLine(string.Format("Server started on {0}. Press enter to close.", uri));
Console.ReadLine();
}
}
}
class Startup
{
static Hub hub;
public void Configuration(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
var configuration = new HubConfiguration();
configuration.EnableDetailedErrors = true;
app.MapSignalR("/signalr", configuration);
hub = new MyHub();
}
}
public class MyHub : Hub
{
public override Task OnConnected() { Console.WriteLine(Context.ConnectionId + " connected"); return base.OnConnected(); }
public override Task OnDisconnected() { Console.WriteLine(Context.ConnectionId + " disconnected"); return base.OnDisconnected(); }
public override Task OnReconnected() { Console.WriteLine(Context.ConnectionId + " reconnected"); return base.OnReconnected(); }
}
}
客户端代码:
using System;
using System.Net;
using Microsoft.AspNet.SignalR.Client;
namespace signalrconnection
{
class Program
{
static void Main(string[] args)
{
var uri = System.Configuration.ConfigurationManager.AppSettings["signalr_uri"] ?? "http://localhost:7890/signalr";
ServicePointManager.DefaultConnectionLimit = 10;
var hubConnection = new HubConnection(uri, false);
hubConnection.StateChanged += stateChange => Console.WriteLine(string.Format("SignalR {0} >> {1} ({2})", stateChange.OldState, stateChange.NewState, hubConnection.Transport == null ? "<<null>>" : hubConnection.Transport.Name));
var hubProxy = hubConnection.CreateHubProxy("MyHub");
hubConnection.Start();
Console.WriteLine("Press enter to die...");
Console.ReadLine();
//hubConnection.Dispose(); //uncomment this to simulate a graceful disconnect which works on both windows and linux
}
}
}
无尽的 Mono 异常:
{path-to-project}/Microsoft.AspNet.SignalR.Core.dll Error : 0 : SignalR exception thrown by Task: System.AggregateException: One or more errors occured ---> System.IO.IOException: Write failure ---> System.Net.Sockets.SocketException: Connection reset by peer
at System.Net.Sockets.Socket.Send (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags) [0x00000] in <filename unknown>:0
at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at System.Net.ResponseStream.InternalWrite (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.Net.ResponseStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
--> (Inner exception 0) System.IO.IOException: Write failure ---> System.Net.Sockets.SocketException: Connection reset by peer
at System.Net.Sockets.Socket.Send (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags) [0x00000] in <filename unknown>:0
at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at System.Net.ResponseStream.InternalWrite (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.Net.ResponseStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
非常感谢任何帮助。先提前致谢!