dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.46k stars 10.03k forks source link

can not use signalr `Clients` in thread? #45656

Closed ljzj2 closed 1 year ago

ljzj2 commented 1 year ago

Is there an existing issue for this?

Describe the bug

I want to use Clients in thread but it throw exception :can not access disposed object.

means the Clients is disposed but in OnConnectionAsync I can use Clients.All.SendAsync() why?

Expected Behavior

I can use the Clients

Steps To Reproduce

create project create SampleHub

create thread

public void PanThread()
{
while(isRunning)
{
await Clients.All.SendAsync("");

Thread.Sleep(1000);
}
}

Exceptions (if any)

can not access disposed object.

.NET Version

7.0

Anything else?

No response

davidfowl commented 1 year ago

Sounds like you are trying to capture the "Clients" property and spin up a thread from OnConnected? I'd recommend instead using a BackgroundService and injecting the IHubContext\<Hub> instead. The hub is no longer usable after the call from the client.

Is there a reason you're spinning up the thread from the hub itself?

ljzj2 commented 1 year ago

I want to create thread in game which run all the time

while(true)
{
await Clients.All.SendAsync("");
}
davidfowl commented 1 year ago

You can see how my game does it:

Wire up the GameState as a singleton: https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Program.cs#L14

The GameState singleton takes an IHubContext<GameServer> - This is how the Game sends updates to the client https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameState.cs

This is the game loop: https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameState.cs#L153

This is how the game state is sent to the client: https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameState.cs#L190

The GameServer Hub injects the GameState: https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameServer.cs#L9

The updates the GameStates:

Add player https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameServer.cs#L18

Update state, based o player keyboard input: https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameServer.cs#L28

Remove player https://github.com/davidfowl/BombRMan/blob/0ed09c551dad5363c6dadd24f320b34e651e6c1e/BombRMan.Core/Hubs/GameServer.cs#L33

Alerinos commented 1 year ago

@ljzj2 If you're making a game, forget about signalr. It is not suitable for live communication. I recommend using gRPC or raw socket. Signal is good for chat, notifications, statuses, etc.

davidfowl commented 1 year ago

@Alerinos not true. SignalR can be used for both things (assuming you have a good architecture 😉 )

Alerinos commented 1 year ago

@davidfowl It all depends on the type of game, if it's a small browser based bomberman game it won't be a problem. I was working on an mmo game project (2000 players online) and fps, here the server is flooded with lots of packets. I don't know if websocket will handle it well. Sometimes the websocket does not contain an http frame that increases the size of the transmitted packets?

davidfowl commented 1 year ago

I was working on an mmo game project (2000 players online) and fps, here the server is flooded with lots of packets. I don't know if websocket will handle it well.

This doesn't sound like a signalr problem, it sounds like an architecture problem.

Sometimes the websocket does not contain an http frame that increases the size of the transmitted packets?

I don't understand what this means. Also, I don't understand how grpc solves this the problem you're talking about.

Regardless, this isn't the issue to discuss this.

ghost commented 1 year ago

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!