Kryonet数据包未发送

时间:2014-06-10 06:52:05

标签: java kryonet

所以我尝试了kryonet,发送了一个自定义数据包,但是我服务器中的监听器似乎无法接收它。

server.addListener(new Listener() {
            @SuppressWarnings("unused")
            public void received(Connection connection, Object object) {
                System.out.println("received");
                if (object instanceof Packet) {
                        Packet p = (Packet) object;
                        System.out.println(p.name);

                }
            }
        });

发送:

            Packet p = new Packet();
        p.name = "test";
        client.sendTCP(p);

通过其他线程阅读,我尝试使用new Thread(client).start();代替client.start();,并且我已将空构造函数添加到我的数据包类中,但客户端要么连接,要么挂起并且永远不会断开连接(如果我使用新的线程(客户端).start())或连接然后立即断开连接(如果我使用client.start())。什么都没有打印过。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

这些是使KryoNet有效的必要步骤:

Server server = new Server();
Kryo kryo = server.getKryo();
kryo.register(float[].class);
server.start();
server.bind(2300, 2301);
server.addListener(new Listener() {
    public void received(Connection connection, Object object)
    {
        if(object instanceof float[])
        {
            float[] array = (float[])object;
        } 
    }
});

Client client = new Client();
Kryo kryo = client.getKryo();
kryo.register(float[].class);
client.addListener(new Listener() {
    public void connected(Connection connection)
    {
        connection.sendTCP(new float[] {5.0f, 6.0f, 7.0f, 8.0f});
    }
});
client.start();
client.connect(5000, "127.0.0.1”, 2300, 2301);

当然,一旦连接到服务器,就可以使用client.sendTCP()。 用Packet替换float [],它应该可以工作。

你不需要自己制作线程,KryoNet已经是异步的,除非你使用它的discoverHost()方法调用。

编辑:我发起了一个有效的示例项目。

Packet.java:

public class Packet
{
    private String message;

    public Packet()
    {
    }

    public Packet(String message)
    {
        this.message = message;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}

KryoClient.java:

public class KryoClient
{
    private Client client;

    public KryoClient() throws IOException
    {
        client = new Client();
        Kryo kryo = client.getKryo();
        kryo.register(float[].class);
        kryo.register(Packet.class);
        //kryo.register(String.class);
        client.addListener(new Listener() {
            public void connected(Connection connection)
            {
                //connection.sendTCP(new float[] {5.0f, 6.0f, 7.0f, 8.0f});
                connection.sendTCP(new Packet("Hello, Server! You're sexy, rawr :3"));
            }

            @Override
            public void received(Connection connection, Object object)
            {
                if (object instanceof float[])
                {
                    float[] array = (float[]) object;
                    for(float a : array)
                    {
                        System.out.println(a);
                    }
                }
                else if(object instanceof Packet)
                {
                    Packet packet = (Packet) object;
                    System.out.println("Message: " + packet.getMessage());
                    connection.sendTCP(new Packet("The packet has arrived to client."));
                }
            }

            @Override
            public void disconnected(Connection arg0)
            {
                System.out.println("Server disconnected.");
            }
        });
        client.start();
        client.connect(5000, "127.0.0.1", 2305, 2306);
    }

    public Client getClient()
    {
        return client;
    }

    public void setClient(Client client)
    {
        this.client = client;
    }
}

KryoServer.java:

public class KryoServer
{
    private Server server;

    public KryoServer() throws IOException
    {
        server = new Server();
        Kryo kryo = server.getKryo();
        kryo.register(float[].class);
        kryo.register(Packet.class);
        //kryo.register(String.class);
        server.start();
        server.bind(2305, 2306);
        server.addListener(new Listener()
        {
            @Override
            public void connected(Connection connection)
            {
                connection.sendTCP(new Packet("Server says, connected to server."));
            }

            @Override
            public void received(Connection connection, Object object)
            {
                if (object instanceof float[])
                {
                    float[] array = (float[]) object;
                    for(float a : array)
                    {
                        System.out.println(a);
                    }
                }
                else if(object instanceof Packet)
                {
                    Packet packet = (Packet) object;
                    System.out.println("Message: " + packet.getMessage());
                    //connection.sendTCP(new Packet("The packet has arrived to server."));
                }
            }

            @Override
            public void disconnected(Connection connection)
            {
                System.out.println("Client disconnected.");
            }
        });
    }

    public Server getServer()
    {
        return server;
    }

    public void setServer(Server server)
    {
        this.server = server;
    }
}

ClientThread.java:

public class ClientThread extends Thread
{
    private KryoClient client;
    private volatile boolean running;

    public ClientThread(KryoClient client)
    {
        this.client = client;
        running = true;
    }

    @Override
    public void run()
    {
        long initTime = System.currentTimeMillis();
        while(running)
        {
            if(System.currentTimeMillis() - initTime > 1000)
            {
                initTime = System.currentTimeMillis();
                client.getClient().sendTCP(new Packet("Hello from " + System.currentTimeMillis()));
                //should have used Thread.sleep(1000); instead
            }
        }
    }

    public void stopThread()
    {
        running = false;
    }
}

MyMain.java:

public class MyMain
{
    private static KryoClient kryoClient = null;
    private static KryoServer kryoServer = null;
    private static ClientThread thread = null;

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = null;
        System.out.println("What's up, doc?");
        System.out.println("Press '1' for server.");
        System.out.println("Press '2' for client.");

        try
        {
            br = new BufferedReader(new InputStreamReader(System.in));
            String line = br.readLine();
            int number = Integer.parseInt(line);
            if (number == 1)
            {
                kryoServer = new KryoServer();
                System.out.println("Server started.");
            }
            else if (number == 2)
            {
                kryoClient = new KryoClient();
                System.out.println("Client started.");
                thread = new ClientThread(kryoClient);
                thread.start();
            }
            System.out.println("Press a button to exit.");
            br.readLine();

            System.out.println("Test end.");
        }
        finally
        {
            if(kryoClient != null)
            {
                kryoClient.getClient().stop();
            }
            if(kryoServer != null)
            {
                kryoServer.getServer().stop();
            }

            if (br != null)
            {
                br.close();
            }
            if (thread != null)
            {
                thread.stopThread();
            }

        }
    }
}

它有效。