Second client that joined the server cannot receive the message
nikitundrik opened this issue · comments
Hello, I'm creating a multiplayer game using HaxeFlixel and this library. Currently, I have a server program that sends a certain message when player joins the game and gives the player a certain ID. The first client gets the message and gets the ID 0, but the second client that joined does not get the message and gets the ID null. I want to get assistance with this issue.
The code in the server that runs when player joins (I know that variable names are bad):
// ...
server.addEventListener(NetworkEvent.MESSAGE_RECEIVED, function(event:NetworkEvent)
{
switch (event.data.case1)
{
case "player_joined":
var previousPlayers = players.members.slice(0, players.members.length - 1);
trace("Previous players: " + previousPlayers);
server.send({case1: "new_player", players1: previousPlayers, playerID: newestID});
trace("Sended");
newestID++;
}
});
// ...
The code in the client that runs when the client receives the message:
// ...
client.addEventListener(NetworkEvent.MESSAGE_RECEIVED, function(event:NetworkEvent)
{
trace("Client received a message");
// If case is new player, add a player
switch (event.data.case1)
{
case "new_player":
trace("Message: new player");
var playersArr:Array<FlxSprite> = event.data.players1;
for (i in playersArr)
{
i.loadGraphic(AssetPaths.PlayerCharacter__png, true, 32, 32);
otherPlayers.add(i);
}
playerID = event.data.playerID;
trace("Player ID: " + playerID);
}
});
// ...
Hi @nikitundrik !
I'll try my best to help you. Could you please create a repository with minimal code to replicate the problem? No need to create sprites, just simple callback handlers and trace
messages. You could pass cli arguments (-host
, -client
) to determine if the application acts either as a client or as a server.
Hi @nikitundrik !
I'll try my best to help you. Could you please create a repository with minimal code to replicate the problem? No need to create sprites, just simple callback handlers and
trace
messages. You could pass cli arguments (-host
,-client
) to determine if the application acts either as a client or as a server.
OK, I will try
Hi @nikitundrik !
I'll try my best to help you. Could you please create a repository with minimal code to replicate the problem? No need to create sprites, just simple callback handlers and
trace
messages. You could pass cli arguments (-host
,-client
) to determine if the application acts either as a client or as a server.
I created projects with minimal code, I ran the server with -host argument and client with -client arguments and in the minimal project it worked normally, but in the main game it's not working normally. Server: https://github.com/nikitundrik/SimpleServer, client: https://github.com/nikitundrik/SimpleClient
If it is working normally on the minimal example, then looks like the library implementation is okay. Perhaps there's is something in your code that is breaking the network flow. Try simplifying your main game's code and see if it works (i.e. only send the desired id on network messages).
Also, what does previousPlayers
include? Is it a sprite array? If it is, perhaps that kind of structure should not be sent over the network. You should limit your network data to primitives (numbers, vectors, colors, strings, etc).
If it is working normally on the minimal example, then looks like the library implementation is okay. Perhaps there's is something in your code that is breaking the network flow. Try simplifying your main game's code and see if it works (i.e. only send the desired id on network messages).
Also, what does
previousPlayers
include? Is it a sprite array? If it is, perhaps that kind of structure should not be sent over the network. You should limit your network data to primitives (numbers, vectors, colors, strings, etc).
Yes, previousPlayers
is a sprite array, I will try sending only locations of a player. Thanks for helping