gpt4 book ai didi

networking - 如何使用 Kryonet 通过网络发送对象?

转载 作者:行者123 更新时间:2023-12-02 04:55:39 25 4
gpt4 key购买 nike

我是网络新手,我正在尝试将我使用 java 创建的棋盘游戏联网。我的一个 friend 向我介绍了 Kryonet 库。到目前为止,它很棒。我不必处理套接字!

我遇到的问题是发送对象。主要是,我有一个 Board 类型的对象。该对象包含其他对象,例如 ArrayList 对象和 Fort 对象。

我尝试只注册 Board 对象,但收到以下错误:

Exception in thread "Server" com.esotericsoftware.kryo.KryoException: java.lang.
IllegalArgumentException: Class is not registered: Game.Tile
Note: To register this class use: kryo.register(Game.Tile.class);
Serialization trace:
t0 (Game.Board)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:571)
at com.esotericsoftware.kryonet.KryoSerialization.write(KryoSerializatio
n.java:50)
at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:192)
etc....

好的,那我也注册Tile.class,

更多错误,但随后我需要注册 ArrayList.class - 所以我注册它,然后又出现更多错误,所以我注册 Fort.class。

当我注册 Fort.class 时,我进入了一个无限循环并得到了很多这样的错误:

    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:504)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:504)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)

这让我相信我不太了解如何正确注册,而且我找不到很多关于如何注册嵌套对象的信息。我的 Fort 类实际上是一个枚举类,但我不确定这是否有所作为?任何帮助将不胜感激!

我在我的大部分网络代码中包含了一个类,因此您可以了解我正在尝试做什么。

这是我的网络代码:

public class Network extends Listener {
private Server server;
private Client client;
private boolean isServer;
private boolean messageReceived;
private PacketMessage message;
private Board board;

public Network(boolean isServer, Board board) throws IOException {
messageReceived = false;
this.board = board;
this.isServer = isServer;
if (isServer) {
initServer();
// receive();
} else {
initClient();
//probably want to run this in main
client();
}
}

private void initServer() throws IOException {
// 127.0.0.1 means myself
// ports up to 1024 are special and reserved
server = new Server();
registerClasses(server.getKryo());
server.bind(8000, 8001);
// starting a new thread
server.start();
// call my received and my connected
server.addListener(this);
}

private void initClient() throws IOException {
// 127.0.0.1 means myself
// ports up to 1024 are special and reserved
client = new Client();
registerClasses(client.getKryo());
// starting a new thread
client.start();
client.connect(5000, "127.0.0.1", 8000, 8001);
// call my received and call my connected
client.addListener(this);

}

//call in main
//
public void client(){
while(true){
sendRequest();
receive();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// tell Kryo what things it's going to have to send
private void registerClasses(Kryo kryo) {
kryo.register(Request.class);
kryo.register(PacketMessage.class);
kryo.register(Fort.class);
kryo.register(ArrayList.class);
kryo.register(Tile.class);
kryo.register(Board.class);
}

private void sendRequest() {
client.sendTCP(new Request());
}

private void receive() {
messageReceived = false;
while (!messageReceived) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// message.message is really packet.message
System.out.println("Received a message from the host: "
+ message.message);
}

public void received(Connection c, Object p) {
System.out.println("Received Message");

// Is the received packet the same class as PacketMessage.class?
if (p instanceof PacketMessage) {
// Cast it so we can access the message within
// PacketMessage packet =(PacketMessage) p;
// System.out.println("Received a message from the host: "+pa cket.message);
message = (PacketMessage) p;
// We have now received the message!
messageReceived = true;
}
else if (p instanceof Request){
// Create a message packet
PacketMessage packetMessage = new PacketMessage();
// Assign the message text
packetMessage.message = "Hello friend! The time is: "
+ new Date().toString();

// Send the message
//probably want another method to send
c.sendTCP(packetMessage);
c.sendTCP(board);
}
}

// This is run when a connection is received!
public void connected(Connection c) {
System.out.println("Received a connection from "
+ c.getRemoteAddressTCP().getHostString());

}

}

最佳答案

可能发生的情况是您的 Fort 类包含 Board 类型的成员,并且此循环引用在序列化 Fort 时导致无限循环。

使用 transient 关键字从序列化中排除成员,或者完全删除循环引用。

关于networking - 如何使用 Kryonet 通过网络发送对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com