### TypeListeners example Source: https://github.com/crykn/kryonet/blob/master/README.md Example demonstrating TypeListeners for handling received messages with lambdas. ```java TypeListener typeListener = new TypeListener(); // add a type handler for SomeRequest.class typeListener.addTypeHandler(SomeRequest.class, (con, msg) -> { System.out.println(msg.getSomeData()); }); // add another one for SomeOtherRequest.class typeListener.addTypeHandler(SomeOtherRequest.class, (con, msg) -> { con.sendTCP(new SomeResponse()); }); server.addListener(typeListener); ``` -------------------------------- ### Registering Classes Source: https://github.com/crykn/kryonet/blob/master/README.md Example of how to register classes with Kryo on both the client and server. ```java Kryo kryo = server.getKryo(); kryo.register(SomeRequest.class); kryo.register(SomeResponse.class); Kryo kryo = client.getKryo(); kryo.register(SomeRequest.class); kryo.register(SomeResponse.class); ``` -------------------------------- ### Getting a Remote Object Proxy for RMI Source: https://github.com/crykn/kryonet/blob/master/README.md Shows how to get a proxy object for a remote object and invoke a method on it. ```java SomeObject someObject = ObjectSpace.getRemoteObject(connection, 42, SomeObject.class); SomeResult result = someObject.doSomething(); ``` -------------------------------- ### Running a server Source: https://github.com/crykn/kryonet/blob/master/README.md Starts a server on TCP port 54555 and UDP port 54777. ```java Server server = new Server(); server.bind(54555, 54777); server.start(); ``` -------------------------------- ### Gradle Dependencies for KryoNet Source: https://github.com/crykn/kryonet/blob/master/README.md Example Gradle configuration to include KryoNet as a dependency using Jitpack. ```gradle allprojects { repositories { // ... maven { url 'https://jitpack.io' } } } dependencies { compile 'com.github.crykn:kryonet:2.22.8' } ``` -------------------------------- ### Adding a listener to handle receiving objects Source: https://github.com/crykn/kryonet/blob/master/README.md Adds a listener to handle receiving objects. Listeners should be added before binding and starting the server. ```java server.addListener(new Listener() { public void received (Connection connection, Object object) { if (object instanceof SomeRequest) { SomeRequest request = (SomeRequest)object; System.out.println(request.text); SomeResponse response = new SomeResponse(); response.text = "Thanks"; connection.sendTCP(response); } } }); ``` -------------------------------- ### Registering Objects and Connections for RMI Source: https://github.com/crykn/kryonet/blob/master/README.md Demonstrates how to register classes, create an ObjectSpace, register objects with an ID, and add connections for RMI. ```java ObjectSpace.registerClasses(endPoint.getKryo()); ObjectSpace objectSpace = new ObjectSpace(); objectSpace.register(42, someObject); // ... objectSpace.addConnection(connection); ``` -------------------------------- ### Discover Host Source: https://github.com/crykn/kryonet/blob/master/README.md Discovers a host on the LAN by broadcasting a UDP message. ```java InetAddress address = client.discoverHost(54777, 5000); System.out.println(address); ``` -------------------------------- ### Class definitions for SomeRequest and SomeResponse Source: https://github.com/crykn/kryonet/blob/master/README.md Class definitions for SomeRequest and SomeResponse. ```java public class SomeRequest { public String text; } public class SomeResponse { public String text; } ``` -------------------------------- ### Setting RMI to Non-Blocking Source: https://github.com/crykn/kryonet/blob/master/README.md Illustrates how to cast a remote object proxy to RemoteObject and set it to non-blocking mode. ```java SomeObject someObject = ObjectSpace.getRemoteObject(connection, 42, SomeObject.class); ((RemoteObject)someObject).setNonBlocking(true); someObject.doSomething(); ``` -------------------------------- ### Connecting a client Source: https://github.com/crykn/kryonet/blob/master/README.md Connects to a server running on TCP port 54555 and UDP port 54777. ```java Client client = new Client(); client.start(); client.connect(5000, "192.168.0.4", 54555, 54777); SomeRequest request = new SomeRequest(); request.text = "Here is the request"; client.sendTCP(request); ``` -------------------------------- ### Adding a listener to print out the response Source: https://github.com/crykn/kryonet/blob/master/README.md Adds a listener to print out the response from the server. ```java client.addListener(new Listener() { public void received (Connection connection, Object object) { if (object instanceof SomeResponse) { SomeResponse response = (SomeResponse)object; System.out.println(response.text); } } }); ``` -------------------------------- ### Set Logging Level Source: https://github.com/crykn/kryonet/blob/master/README.md Sets the logging level for KryoNet using the MinLog library. ```java Log.set(LEVEL_TRACE); ``` -------------------------------- ### Retrieving Return Value or Exception from Non-Blocking RMI Source: https://github.com/crykn/kryonet/blob/master/README.md Demonstrates how to retrieve the return value or exception from a non-blocking remote method invocation using waitForLastResponse. ```java RemoteObject remoteObject = (RemoteObject)someObject; remoteObject.setNonBlocking(true); someObject.doSomething(); // ... SomeResult result = remoteObject.waitForLastResponse(); ``` -------------------------------- ### Retrieving Specific Response from Non-Blocking RMI Source: https://github.com/crykn/kryonet/blob/master/README.md Shows how to retrieve a specific return value or exception from a non-blocking remote method invocation using waitForResponse with a response ID. ```java RemoteObject remoteObject = (RemoteObject)someObject; remoteObject.setNonBlocking(true); someObject.doSomething(); byte responseID = remoteObject.getLastResponseID(); // ... SomeResult result = remoteObject.waitForResponse(responseID); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.