Hey,
I have started using the Kryonet lib and I have some trouble with the network communication.
I followed the kryonet instruction, but either the server does not send a package or the client does not receive anything.
Client:
if (!Connection.load())
System.exit(0);
public class Connection {
static Client client;
static boolean load() {
try {
client = new Client();
client.start();
client.connect(5000, "localhost", 54555, 54777);
Kryo kryo = client.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
Request request = new Request();
request.text = "Here is the request!";
client.sendTCP(request);
client.addListener(new Listener() {
@SuppressWarnings("unused")
public void received(Connection c, Object o) {
Sys.alert("A", "B"); // never called!
if (o instanceof Response) {
Response response = (Response) o;
Out.say(response.text);
}
}
});
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Server:
Server server = new Server();
server.start();
server.bind(54555, 54777);
Kryo kryo = server.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
server.addListener(new Listener() {
public void received(Connection c, Object o) {
if (o instanceof Request) {
Request request = (Request) o;
System.out.println(request.text); // Here is the request
Response response = new Response();
response.text = "Thanks!";
System.out.println(c.sendTCP(response)) // 11;
}
}
});