0
嗨我正在使用Kryonet作爲我正在開發的遊戲的網絡庫。Kryonet連接成功但沒有收到消息
我有了這個代碼在服務器端:
public class MyServer {
Kryo kryo;
Server server;
Connection connection;
public MyServer(){
server = new Server();
server.start();
try {
server.bind(59990, 59900);
} catch (IOException e) {
System.out.println("FATAL ERROR");
}
kryo = server.getKryo();
kryo.register(Message.class);
server.addListener(new Listener() {
public void received (Connection con, Message str) {
System.out.println("Message recieved from client to server");
Message m = (Message)str;
System.out.println(m.text);
}
public void connected(Connection con){
System.out.println("Hey, we are connected!");
connection = con;
con.sendTCP("HOLA CLIENTE!!");
}
public void disconnected(Connection con){
System.out.println(":(He is gone... he'll never come back..");
}
});
}
public void sendMessage(String tipoMensaje,String tipoTropa,String id){
connection.sendTCP(tipoMensaje+"-"+tipoTropa+"-"+id);
}
}
這在客戶端:
public class MyClient {
Client client;
Connection connection;
Kryo kryo;
public MyClient(){
client = new Client();
client.start();
try {
kryo = client.getKryo();
kryo.register(Message.class);
client.connect(5000, "10.211.55.3", 59990, 59900);
//sendMessage("fromclient","ee","ee");
client.addListener(new Listener() {
public void received (Connection con, String str) {
System.out.println(str);
System.out.println("Message recieved from server to client");
}
public void connected(Connection con){
System.out.println("Hey, we are connected!");
connection = con;
Message m = new Message();
m.text="eeep";
client.sendTCP(m);
}
public void disconnected(Connection con){
System.out.println(":(He is gone... he'll never come back..");
}
});
} catch (IOException e) {
System.out.println("Error while connecting");
}
}
public void sendMessage(String tipoMensaje,String tipoTropa,String id){
client.sendTCP(tipoMensaje+"-"+tipoTropa+"-"+id);
System.out.println("Message sent from client to server");
}
}
消息只是在一個字符串類。當我嘗試將客戶端連接到服務器時,我收到「嗨,我們已連接!」消息,然後在服務器的命令行中輸入「從客戶端接收到服務器的消息」。但是,m.text(消息的內容)未打印,我不知道爲什麼。
在此先感謝。