So far, we've learned the basics to set up a server and connecting a client. However, they don't do much, so let's look into what it takes to get them to communicate with each other.
In SpiderMonkey, communication is handled via messaging and the message interface. When a server sends a message, it uses the broadcast()
method, while a client uses send()
. The side that is supposed to receive the message has to have a suitable MessageListener
class. To try all these things out, let's have our server greet the connecting player by sending them a message, which will be displayed once received.
Perform the following steps to connect and handle basic messaging:
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; } ...