SignalR - Turnbased Game with only two persons per group












-1















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");
}
});

});









share|improve this question



























    -1















    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");
    }
    });

    });









    share|improve this question

























      -1












      -1








      -1








      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");
      }
      });

      });









      share|improve this question














      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");
      }
      });

      });






      c# signalr multiplayer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 11:10









      captainlowiecaptainlowie

      1816




      1816
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to handle your different groups in your code, every players will be on the same SignalR hub, then your application must handle the groups. I suggest you to read the documentation about Groups : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups



          The examples are quite explicit :



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

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


          Of course you have to maintain a List of rooms, then when a new player arrives look into this list to find an available room or create a new one.






          share|improve this answer
























          • thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

            – captainlowie
            Jan 2 at 12:33








          • 2





            You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

            – Leogiciel
            Jan 2 at 12:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005254%2fsignalr-turnbased-game-with-only-two-persons-per-group%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to handle your different groups in your code, every players will be on the same SignalR hub, then your application must handle the groups. I suggest you to read the documentation about Groups : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups



          The examples are quite explicit :



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

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


          Of course you have to maintain a List of rooms, then when a new player arrives look into this list to find an available room or create a new one.






          share|improve this answer
























          • thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

            – captainlowie
            Jan 2 at 12:33








          • 2





            You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

            – Leogiciel
            Jan 2 at 12:38
















          1














          You have to handle your different groups in your code, every players will be on the same SignalR hub, then your application must handle the groups. I suggest you to read the documentation about Groups : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups



          The examples are quite explicit :



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

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


          Of course you have to maintain a List of rooms, then when a new player arrives look into this list to find an available room or create a new one.






          share|improve this answer
























          • thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

            – captainlowie
            Jan 2 at 12:33








          • 2





            You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

            – Leogiciel
            Jan 2 at 12:38














          1












          1








          1







          You have to handle your different groups in your code, every players will be on the same SignalR hub, then your application must handle the groups. I suggest you to read the documentation about Groups : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups



          The examples are quite explicit :



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

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


          Of course you have to maintain a List of rooms, then when a new player arrives look into this list to find an available room or create a new one.






          share|improve this answer













          You have to handle your different groups in your code, every players will be on the same SignalR hub, then your application must handle the groups. I suggest you to read the documentation about Groups : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups



          The examples are quite explicit :



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

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


          Of course you have to maintain a List of rooms, then when a new player arrives look into this list to find an available room or create a new one.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 11:58









          LeogicielLeogiciel

          15810




          15810













          • thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

            – captainlowie
            Jan 2 at 12:33








          • 2





            You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

            – Leogiciel
            Jan 2 at 12:38



















          • thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

            – captainlowie
            Jan 2 at 12:33








          • 2





            You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

            – Leogiciel
            Jan 2 at 12:38

















          thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

          – captainlowie
          Jan 2 at 12:33







          thank you, but it doesn't really help me. I already read the documentation but cannot understand it. Frankly i'm unable to understand how to implement groups within my javascript code.

          – captainlowie
          Jan 2 at 12:33






          2




          2





          You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

          – Leogiciel
          Jan 2 at 12:38





          You have to handle the groups in your C# code. Javascript won't know of client's group, the server will handle this based on the connection ID of your client.

          – Leogiciel
          Jan 2 at 12:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005254%2fsignalr-turnbased-game-with-only-two-persons-per-group%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory