http://s000.tinyupload.com/index.php?file_id=02123651561491845336
I spent the last few hours building an automatic server. It makes having shared objects between Client/Server very simple. It also uses reflection to detect changed variables and then sends the updates to everyone else that is connected.
It has some holes as of now, but I thought it was a very interesting idea.
It’s not very secure so I wouldn’t recommend using it for a real server. But if you need to test a server/client quickly, this can easily facilitate that.
All Objects that are to be shared between the server/clients extend the SharedObject class. Then you call the init class and then you no longer have to worry about sending updates of the variables to the server/clients. It’s done automatically for you.
public class Player extends SharedObject {
private static final long serialVersionUID = 1L;
public int Health = 20;
public int Stamina = 10;
public int Level = 1;
public Player(String name) {
this.identity = name;
this.registerKeys("Health", "Stamina", "Level");
}
}
public class MyGame implements Runnable {
private ConnectionToServer server;
private Player player;
public MyGame() {
try {
System.out.println("Attempting to connect to server.");
Socket socket = new Socket("localhost", 2240);
server = new ConnectionToServer(socket);
System.out.println("Connected to server.");
String name = JOptionPane.showInputDialog("What's your name?");
player = new Player(name);
player.init(server);
} catch(Exception e) {
System.out.println("Failed to connect to server.");
}
}
}
After calling the registerKeys on Field names, and calling the init(PacketCommunicator) function on a shared object, it’s all taken care of for you.
To get it running, just import it into eclipse, and then run 2 applications: 1) MyAutoServer 2) MyGame
Comments would be much appreciated!