Piotrek
Piotrek

Reputation: 11221

Session data in SignalR: Where to store them?

Let's assume that I have a game which uses SignalR to share information between clients. In my Hub class I have a method called "joinGame()" which is called when some client wants to join. I need to add his ID to some array, to know that he has already joined the game. It doesn't need to be in database, because I need this information only during this session.

public class GameHub : Hub
{
    public List<int> Players = new List<int>();

    public void joinGame(int id) //player updates his position
    {
         Clients.All.newPlayer(id);
         Players.Add(id);
    }
}

This code won't work, because "Players" variable seems to be cleared every time I call "joinGame()" function.

How can I do it properly?

Upvotes: 3

Views: 2080

Answers (1)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

You can use Groups:

public async Task JoinGame(int id)
{
    await Groups.Add(Context.ConnectionId, id);
    Clients.All.newPlayer(id);
}

Upvotes: 3

Related Questions