gpt4 book ai didi

c# - SignalR Owin Self-Host on Linux/Mono SocketException 当客户端失去连接时

转载 作者:IT王子 更新时间:2023-10-29 00:25:04 27 4
gpt4 key购买 nike

我在 ubuntu 服务器 14.04、mono 3.2.8 上运行非常简单的信号器服务器,通过 Owin 自托管。 (下面的代码)。

连接/断开连接在远程 Windows 服务器和我将这些位部署到 Linux 服务器时都可以正常工作。但是当一个客户端意外死亡而不是告诉信号器他正在断开连接时,那是我只在 linux 服务器上得到一个永无止境的 SocketException 的时候。 Windows 服务器在大约 30 秒后断开客户端连接,但 linux 服务器每隔 10 秒左右就会永远喷出 socketexception(也在下面)。

我怎样才能使 linux 服务器在运行相同代码时表现得像 windows 服务器一样,在设置的超时后断开用户连接并且不抛出套接字异常?

服务器代码:

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
}
}
}

永无止境的单声道异常:

{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

任何帮助将不胜感激。提前致谢!

最佳答案

问题是这一行 static Hub hub,以及启动方法中的这一行 hub = new MyHub()

您不需要显式创建 Hub 类的实例,也不需要保留引用。集线器类在每次向服务器发出请求时实例化。看 http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#transience关于Hub 对象生命周期 的部分。

关于c# - SignalR Owin Self-Host on Linux/Mono SocketException 当客户端失去连接时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com