JGN.createThread(client).start(); //create a new thread for the client and start it
JMEGraphicalController controller = new JMEGraphicalController(); //in charge of generating and applying sync messages
//create the sync manager, register ourselves with it, and start its thread
SynchronizationManager syncManager = new SynchronizationManager(client, controller);
syncManager.addSyncObjectManager(this);
JGN.createThread(syncManager).start();
//connect to the server
System.out.println("Connecting...");
try {
client.connectAndWait(serverReliable, serverFast, 5000);
} catch (IOException e) {
//Show an error message that we couldn't connect and print detailed info to standard error
JOptionPane.showMessageDialog(null, "Could not connect to server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
System.err.println("Error while connecting to server:");
e.printStackTrace();
return;
} catch (InterruptedException e) {
//Show an error message that we couldn't connect and print detailed info to standard error
JOptionPane.showMessageDialog(null, "Could not connect to server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
System.err.println("Error while connecting to server:");
e.printStackTrace();
return;
}
System.out.println("Connected!");
//attach a MessageListener so that we can get more detailed information about what the server is doing
client.getServerConnection().getReliableClient().addMessageListener(new MessageListener() {
public void messageCertified(Message message) {
System.out.println("Message Certified: " + message);
}
public void messageFailed(Message message) {
System.out.println("Message Failed: " + message);
}
public void messageReceived(Message message) {
System.out.println("Message Received: " + message);
}
public void messageSent(Message message) {
System.out.println("Message Sent: " + message);
}
});
//register our sphere and make sure ot include its color
try {
syncManager.register(localSphere, new ColoredSynchronizeCreateMessage(localSphere.getColor()), 50);
} catch (IOException e) {
System.err.println("Could not register local PlayerSphere:");
e.printStackTrace();
return;
}