SignalR - Turnbased Game with only two persons per group
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
add a comment |
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
add a comment |
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
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
c# signalr multiplayer
asked Jan 2 at 11:10


captainlowiecaptainlowie
1816
1816
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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