SignalR(.NET Core)与慢网络的问题

huangapple go评论76阅读模式
英文:

SignalR (.NET Core) problems with slow internet

问题

以下是翻译好的部分:

我正在使用.NET SignalR在在线多人游戏中。

当玩家离开游戏时,我会用机器人替代他,以便其他玩家可以继续游戏。

public class MyHub : Hub
{
    private readonly MyService myService;

    public MyHub(MyService myService)
    {
        this.myService = myService;
    }

    public string GetConnectionId() => this.Context.ConnectionId;

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await this.myService.HandlePlayerLeave(this.Context.ConnectionId); // 用机器人替换玩家
        await base.OnDisconnectedAsync(exception);
    }
}

然而,一些来自网络连接较慢国家的玩家报告说,有时游戏对他们来说会停止,出现了与他们离开游戏相同的行为,但实际上他们并没有离开。

我有以下问题:

我不明白为什么SignalR在某些人因网络连接很慢而导致游戏停止时会表现得与用户退出游戏时一样。在这些情况下,用户告诉我连接从未断开。有人能解释这种行为吗?

我该如何重现这种行为?我尝试在浏览器中使用限速,但没有帮助。

我该如何解决这个问题并优化对网络连接较慢的用户的行为?

英文:

I'm using .NET SignalR in an online multiplayer game.

When a player leaves the game, I replace him with a robot so that other players can continue gaming.

public class MyHub : Hub
{
    private readonly MyService myService;

    public MyHub(MyService myService)
    {
        this.myService = myService;
    }

    public string GetConnectionId() => this.Context.ConnectionId;

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await this.myService.HandlePlayerLeave(this.Context.ConnectionId); // replace player with robot
        await base.OnDisconnectedAsync(exception);
    }
}

However, some players from countries with slow network connections are reporting that sometimes the game stops for them, experiencing the same behavior as if they had left the game, but they did not.

I have the following questions:

I don't understand why SignalR behaves the same way as when a user exits the game when someone has a very slow internet connection. In these cases, the users tell me that the connection never breaks up. Can someone explain this behavior?

How can I reproduce this behavior? I tried using throttling in the browser, but it didn't help.

How can I solve this problem and optimize the behavior for users with slow internet connections?

答案1

得分: 1

你可以使用 SignalR 配置 来更改连接行为。增加 ClientTimeoutIntervalHandshakeTimeoutKeepAliveInterval 的值,以查看是否满足您的需求。应仔细阅读每个选项的描述,因为其中一些选项相互依赖。查看以下 链接 获取更多选项

services.AddSignalR().AddHubOptions<MyHub>(options =>
{
 options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
 options.KeepAliveInterval = TimeSpan.FromSeconds(45);
});

此外,当某人试图在低互联网连接下上传大文件时,我遇到了相同的问题。通过更改 kestrel 服务器选项,我能够修改数据传输速率。

builder.WebHost.ConfigureKestrel(serverOptions =>
{
 serverOptions.Limits.MinRequestBodyDataRate =
  new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(15));
});
英文:

You can use SignalR configuration to change connection behaviors. Increase ClientTimeoutInterval, HandshakeTimeout, and KeepAliveInterval values to see if it meets your requirements. Each option description should be read carefully, as some of them depend on each other. check the following link for more options.

services.AddSignalR().AddHubOptions&lt;MyHub&gt;(options =&gt;
{
 options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
 options.KeepAliveInterval = TimeSpan.FromSeconds(45);
});

Also, I had the same issue uploading large files when someone was trying to upload a file with a low internet connection. By changing kestrel server options I was able to modify data rates.

builder.WebHost.ConfigureKestrel(serverOptions =&gt;
{
serverOptions.Limits.MinRequestBodyDataRate =
 new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(15));
});

huangapple
  • 本文由 发表于 2023年7月3日 16:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定