Game Development Reference
In-Depth Information
How to do it...
Perform the following steps to connect and handle basic messaging:
1. We begin by defining our message. It's a simple serializable bean with just one
field, as shown in the following code snippet:
@Serializable()
public class ServerMessage extends AbstractMessage{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2. Next, we create a class that implements MessageListener . It's a very simple
class that will print the message to the console when received, as follows:
public class ServerMessageHandler implements
MessageListener<Client>{
public void messageReceived(Client source, Message
m) {
ServerMessage message = (ServerMessage) m;
System.out.println("Server message: " +
message.getMessage());
}
}
3. We instantiate ServerMessageHandler and add it to the client, telling it to
only listen for ServerMessages , as follows:
ServerMessageHandler serverMessageHandler = new
ServerMessageHandler();
Search WWH ::




Custom Search