I want to create a Connect Four multiplayer game with SignalR. I'm able to play against another player on two different devices, but if a third person connects nothing works like supposed to. My main problem is to create a SignalR Group with the limit of 2 connections. If a third person connects it should create a new group rather than entering the first group. Also i don't understand how use groups within my javascript code.
I tried to store every connectionID in a hashset but don't know how to create a new group every time the number of connections is odd.
public static class UserHandler
{
public static HashSet<string> ConnectedIds = new HashSet<string>();
}
public class OnlinegameHub : Hub
{
public void SendUserInformation(string username, int colorCode)
{
Clients.Others.getUserInformation(username, colorCode);
}
public void SendTurn(int col, int colorCode, string yourColor)
{
Clients.Others.updateBoard(col, colorCode, yourColor);
}
public Task JoinLobby(string groupName)
{
return Groups.Add(Context.ConnectionId, groupName);
}
public override Task OnConnected()
{
UserHandler.ConnectedIds.Add(Context.ConnectionId);
return base.OnConnected();
}
}
//Javascript part
$.connection.hub.start().done(function () {
game.server.sendUserInformation(playerOneUsername, yourCode);
$(".column").click(function () {
if (yourTurn) {
game.server.sendTurn($(this).index(), yourCode, yourColor);
insertColorCode(yourCode, $(this).index());
yourTurn = false;
$(".turn-text").html("It's " + playerTwoUsername + "'s turn");
}
});
});