Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Handling basic messaging


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.

Getting ready

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.

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;
        }
    
    ...