Question

I'm creating a web browser Reversi game. I use SignalR for communication. I've already implemented 2 side communication between server and one client. Now I encountered a problem.

There are multiple games going on at the same time. There are 2 players in each game. I want to use an asynchronous communication with push notifications. More precisely when I decide there's time no notify players about something I want to push the notification to them. But not to all the players, only to the two connected to the certain game.

So far (to send data back to the caller) I do it like that:

Clients.Caller.GameStatusUpdated(status);

This way I can't call the second player. I thought I could just store the Clients.Caller objects, but I found out that every call (even from the same client hub, still connected) creates a new instance of the server hub. The hashcode of Clients.Caller is also different every time.

What's the right way to go here?

Was it helpful?

Solution 2

You can take a loot at groups

When an user join the game, you just add it to a group:

public Task JoinRoom(string roomName)
{
    return Groups.Add(Context.ConnectionId, roomName);
}

Just make sure that you have one group per game (ie one roomName per game).

Then you can communicate with all the users in the same group (in our case, roomName)

Clients.Group(roomName).GameStatusUpdated(status)

To leave a group:

public Task LeaveRoom(string roomName)
{
    return Groups.Remove(Context.ConnectionId, roomName);
}

Groups are not the only solution. You can use in-memory mapping or database mapping. Take a look here.

OTHER TIPS

I think SignalR groups will solve your problem. I think you'll want to add each player of each individual game to a unique group. Then you'll be able to use Clients.Group(game). GameStatusUpdated(status) to tell just the individual game group about the updates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top