Kryo是一个序列化框架。
Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http。
自己写了一个测试代码,运行了下,感觉还不错,记录下来。
1、listener
package com.mytestcodes.kryonet; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener; import com.mytestcodes.serialization.fulltest.Child; public class KListener extends Listener { public KListener() { } public KListener(String name) { this.name = name; } private String name; public void received(Connection connection, Object object) { System.out.println(name + " has recive a message"); if (object instanceof Child) { Child child = (Child) object; System.out.println(child.getChildName()); child.setChildName(name + " response"); connection.sendTCP(child); } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、client
3、server
package com.mytestcodes.kryonet; import java.io.IOException; import com.esotericsoftware.kryonet.Server; public class KServer { public static void main(String[] args) { Server server = new Server(); server.addListener(new KListener("server")); server.getKryo().setRegistrationOptional(true); server.start(); try { // TCP port 54555 and UDP port 54777 server.bind(54555, 54777); } catch (IOException e) { e.printStackTrace(); } } }
4、child:
package com.mytestcodes.serialization.fulltest; import java.io.Serializable; public class Baby implements Serializable { /** * */ private static final long serialVersionUID = 8882758100866916676L; private String babyName = "baby"; public String getBabyName() { return babyName; } public void setBabyName(String babyName) { this.babyName = babyName; } }
package com.mytestcodes.serialization.fulltest; import java.io.Serializable; public class Parent implements Serializable { /** * */ private static final long serialVersionUID = 6933088125784071832L; private String parentName="parent"; public String getParentName() { return parentName; } public void setParentName(String parentName) { this.parentName = parentName; } }