gpt4 book ai didi

c# - SignalR 服务器在客户端连接时立即关闭连接

转载 作者:行者123 更新时间:2023-12-04 12:59:26 24 4
gpt4 key购买 nike

我想在项目中使用 SignalR 向设备发送重启消息,但我不能。所以我决定开始一个新的解决方案,仅用于测试目的,结果相同:

SocketException (0x80004005): 现有连接被远程主机强行关闭

我正在使用带有 .NET 4.7.2 的 Visual Studio Professional 2017,这些是我遵循的步骤:

  • 我使用名为 SignalRServer 的 ASP.NET Web App 项目创建了一个新解决方案, 使用 Indentity 进行身份验证。
  • 我创建名为 SignalR 的新文件夹并添加新项目 > SignalR Hub Class (v2)。我给它取名 SignalRHub :
  • public class SignalRHub : Hub
    {
    public void Reboot()
    {
    Clients.All.reboot();
    }
    public void Hello()
    {
    Clients.All.hello();
    }
    public override Task OnConnected()
    {
    Debug.WriteLine($"[SERVER] {this.Context.ConnectionId} connected");
    return base.OnConnected();
    }
    public override Task OnReconnected()
    {
    Debug.WriteLine($"[SERVER] {this.Context.ConnectionId} reconnected");
    return base.OnReconnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
    Debug.WriteLine($"[SERVER] {this.Context.ConnectionId} disconnected");
    return base.OnDisconnected(stopCalled);
    }
    }
  • 在 Startup.cs 文件中:
  •     public partial class Startup
    {
    public void Configuration(IAppBuilder app)
    {
    app.MapSignalR();
    ConfigureAuth(app);
    }
    }

    服务器实现完成。之后,客户端:
  • 我添加了一个名为 SignalRClient 的新 WPF 项目.
  • 我加Microsoft.AspNet.SignalR.Client使用 Nuget 打包。
  • 我换MainWindow.xaml :
  • <Window x:Class="SignalRClient.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SignalRClient"
    mc:Ignorable="d"
    Title="MainWindow" Height="300" Width="400" Loaded="Window_Loaded">
    <Grid>
    <Button VerticalAlignment="Center" HorizontalAlignment="Center" Padding="5" Content="Send message!" Click="Button_Click"/>
    </Grid>
    </Window>
  • 最后我换了 MainWindow.xaml.cs :
  • public partial class MainWindow : Window
    {
    private IHubProxy signalRHubProxy;

    public MainWindow()
    {
    InitializeComponent();
    }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
    try
    {
    using (var hubConnection = new HubConnection("http://localhost:54475/"))
    {
    this.signalRHubProxy = hubConnection.CreateHubProxy("SignalRHub");
    this.signalRHubProxy.On("Hello", this.HelloMessageReceived);
    this.signalRHubProxy.On("Reboot", this.RebootMessageReceived);
    ServicePointManager.DefaultConnectionLimit = 10;
    await hubConnection.Start();
    Debug.WriteLine($"[CLIENT] Connected to hub");
    }
    }
    catch (Exception exception)
    {
    Debug.WriteLine($"[CLIENT] {exception.Message}");
    }
    }

    private void HelloMessageReceived()
    {
    Console.WriteLine("[CLIENT] Hello message received");
    }
    private void RebootMessageReceived()
    {
    Console.WriteLine("[CLIENT] Reboot message received");
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
    await this.signalRHubProxy.Invoke("Hello", new { });
    }
    }

    客户端也完成了。现在我首先运行 Web 应用程序,当加载主页时,我运行 WPF 应用程序(调试 > 启动新实例)。这是输出:
    [SERVER] b3f11c77-6bfc-416a-914b-f2573f0cc42c connected
    [CLIENT] Connected to hub
    [SERVER] b3f11c77-6bfc-416a-914b-f2573f0cc42c disconnected
    SignalRClient.exe Error: 0 : Error while closing the websocket: System.Net.WebSockets.WebSocketException (0x80004005): An internal WebSocket error occurred. Please see the innerException, if present, for more details. ---> System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
    at System.Net.WebSockets.WebSocketConnectionStream.WebSocketConnection.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    at System.Net.DelegatedStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    at System.Net.WebSockets.WebSocketConnectionStream.<>n__1(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
    at System.Net.WebSockets.WebSocketConnectionStream.<WriteAsync>d__22.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Net.WebSockets.WebSocketBase.<SendFrameAsync>d__48.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
    at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__51.MoveNext()
    at System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException(String methodName, Exception exception, CancellationToken cancellationToken, Boolean aborted)
    at System.Net.WebSockets.WebSocketBase.<CloseOutputAsyncCore>d__51.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler.<>c.<<CloseAsync>b__13_0>d.MoveNext() in /_/src/Microsoft.AspNet.SignalR.Core/Owin/WebSockets/WebSocketHandler.cs:line 114
    The thread 0x4b44 has exited with code 0 (0x0).
    The thread 0x5560 has exited with code 0 (0x0).

    我可以看到客户端连接到服务器,但连接立即关闭。我不知道我是不是忘记了一步。我以相同的结果停止了防火墙。你知道我该如何解决这个问题吗?

    最佳答案

    好像using声明是它处理创建的原因 HubConnection try末尾的对象导致 websocket 关闭的块。

    您可以考虑删除 using声明和保存 hubConnection作为字段或属性调用 hubConnection.Stop()稍后断开连接。

    关于c# - SignalR 服务器在客户端连接时立即关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60564697/

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