Networking can basically be like :
Client sends “i shoot”
Server (calculates the location / rotation of bullet)
Server broadcasts “created bullet @ (x,y,r)”
Client creates bullet
…
But this way the game will appear laggy for all the clients (try it on a multiplayer game on Age OF Empire II e.g.). The fact is that every single action for the player is going to be asserted by the server.
Facing this lag issue, the solution is to locally admit your action is valid. e.g :
Client creates bullet + sends “i shoot”
Server tests validity of the input/action
Server broadcasts everyone BUT THIS CLIENT “new bullet @ (x,y,r)” [this way action is valid]
OR
Server answers Client “delete this bullet” [this way, action is not valid]
I don’t know if there is an implementation of broadcasting on server, but my way to handle this would be in the ServerListenner class:
public class PersonnalListenner extends ServerEventListener {
private List<ClientModel> clients = new List<ClientModel>();
protected void packetReceived(ClientModel client, Packet packet) {
}
@Override
protected void errorMessage(String msg) {
}
@Override
protected void debugMessage(String msg) {
}
@Override
protected void clientDisconnected(ClientModel client) {
clients.remove(client);
}
@Override
protected void clientConnected(ClientModel client) {
clients.add(client);
}
public void broadcast(Packet p) {
//sends the packet to each client connected
for(ClientModel c : clients){
c.sendPacket(p);
}
}
public void broadcastExcepted(ClientModel excepted, Packet p) {
for(ClientModel c : clients){
if(c == excepted){
continue; //skips the "broadcast" behaviour if ClientModel in param is encountered
}
c.sendPacket(p);
}
}
}
I didn’t try to run this code (i’m not working over this for now). Also think about synchronizing every piece of code arround accessing “clients” list.
Sorry for the bad english :P. I hope i’m understandable.