-
Notifications
You must be signed in to change notification settings - Fork 5
Client Setup
The Netty-Money client was built on Netty.IO and is designed to run as a jMonekyEngine app state. It will establish a connection to the server with both a TCP and UDP channel. The client has built in features for reconnecting to the server given the loss of connection, message caching, using SSL for TCP traffic, and managing custom connection and message logic.
To get started, you need to create a client and attach it to your jMonkeyEngine app.
public class JmeTestClient extends SimpleApplication {
@Override
public void simpleInitApp() {
NettyClient client = new NettyClient("test", 10000, "localhost");
stateManager.attach(client);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
JmeTestClient client = new JmeTestClient ();
client.start();
}
}
Here we have created a client with the service name test
and it will attempt to connect to a server running at host localhost
on port 10,000.
Now that we have a client, we need to be able to use it. Monkey-Netty has two listeners that can be registered or unregistered from the client at any time. ConnectionListener
and MessageListener
are used to add custom logic to the client.
The connection listener will let you know when the client has connected or disconnected from the server. You can add a connection listener simply by registering it.
public class JmeTestClient extends SimpleApplication {
@Override
public void simpleInitApp() {
NettyClient client = new NettyClient("test", 10000, "localhost");
client.registerListener(new ConnectionListener() {
@Override
public void onConnect(NetworkClient client) {
System.out.println("Client connected to server");
}
@Override
public void onDisconnect(NetworkClient client) {
System.out.println("Client disconnected from server");
}
});
stateManager.attach(client);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
JmeTestClient client = new JmeTestClient ();
client.start();
}
}
The message listener allows you to listen for messages from the server. You can add a message listener simply by registering it with the client.
public class JmeTestClient extends SimpleApplication {
@Override
public void simpleInitApp() {
NettyClient client = new NettyClient("test", 10000, "localhost");
client.registerListener(new MessageListener() {
@Override
public void onMessage(NetworkMessage msg, NetworkServer server, NetworkClient client) {
System.out.println("Got message " + msg);
}
@Override
public Class<? extends NetworkMessage>[] getSupportedMessages() {
return new Class[] {TestTCPMessage.class, TestUDPMessage.class};
}
});
stateManager.attach(client);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
JmeTestClient client = new JmeTestClient ();
client.start();
}
}
Here we listen for messages and print the name of the message we receive. Take note the getSupportedMessages()
, it allows us to add a filter for what messages we want this listener to listen for. The onMessage(NetworkMessage msg, NetworkServer server, NetworkClient client)
function gives us the message, server, and client that are related to the message. On the client side, the server
parameter will always be null`.
The client comes with several other useful features.
The message cache will store messages that need to go to the server in the event that we are not connected to the server. This could be for messages that were requested to be sent prior to the client making its initial connection, or messages that we want to send but the client temporarily lost connection. When the client resumes its connection to the server, these messages will be sent in the order they were received before sending any other messages.
By default the cache only stores TCP messages, you can control how the cache works by setting the cache mode:
public class JmeTestClient extends SimpleApplication {
@Override
public void simpleInitApp() {
NettyClient client = new NettyClient("test", 10000, "localhost");
client.setMessageCacheMode(MessageCacheMode.ENABLED);
stateManager.attach(client);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
JmeTestClient client = new JmeTestClient ();
client.start();
}
}
Here I have enabled it for both UDP and TCP. Mode options are:
DISABLED - Don't cache anything
ENABLED - Cache both TCP and UDP
TCP_ENABLED - Cache only TCP
UDP_ENABLED - Cache only UDP
The client contains an internal logger that can be enabled or disabled for debugging.
public class JmeTestClient extends SimpleApplication {
@Override
public void simpleInitApp() {
NettyClient client = new NettyClient("test", 10000, "localhost");
client.setLogLevel(LogLevel.DEBUG);
stateManager.attach(client);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
JmeTestClient client = new JmeTestClient ();
client.start();
}
}
See Server SSL Support for info on setting up ssl.