简体   繁体   English

Kryonet连接成功,但未收到消息

[英]Kryonet connection successful but not receiving messages

Hi I am using Kryonet as my network library for the game I'm developing. 嗨,我正在使用Kryonet作为我正在开发的游戏的网络库。

I've got this code in the server side: 我在服务器端有以下代码:

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);
    }

}

And this in the client side: 而在客户端:

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");

    }
}

Message is just a class with a string in it. 消息只是其中包含字符串的类。 When I try to connect client to the server I get the "Hey, we are connected!" 当我尝试将客户端连接到服务器时,出现“嘿,我们已连接!” message and afterwards a "Message recieved from client to server" in the server's command line. 消息,然后在服务器的命令行中显示“从客户端到服务器的消息接收”。 However, m.text (the content of the message) is not printed and I don't know why. 但是,m.text(消息的内容)未打印,我也不知道为什么。

Thanks in advance. 提前致谢。

It's because there is no method public void received (Connection con, String str) in a Listener - basically, your server never calls it. 这是因为在侦听器中没有public void received (Connection con, String str)方法public void received (Connection con, String str) -基本上,您的服务器从不调用它。 This is also a reason why you should use @Override - you wouldn't have this problem in the first place. 这也是您应该使用@Override的原因-首先您不会遇到此问题。

Replace String str with Object o . String str替换为Object o Then - check if your object is actually a Message . 然后-检查您的对象是否实际上是Message If it is, cast it to a Message and print out its text. 如果是这样,请将其转换为Message并打印出其文本。 Your method should look like this: 您的方法应如下所示:

@Override
public void received(Connection connection, Object packet) {
    if (packet instanceof Message) {
        System.out.println(((Message) packet).text);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM