java - Kryonet 客户端超时后断开连接

标签 java client libgdx kryonet

当我使用这条线路进行连接时,我的 Kryonet 服务器在 5000 毫秒后断开连接 client.connect(5000, 主机, Network.port); 我以为 5000 是连接超时,但当我运行连接时,它能够连接并接收我发送的类,但在 5000 毫秒后与服务器断开连接。

我正在修改 Kryonet 提供的基本 ChatClient.java。这是我的想法。

import java.awt.EventQueue;
import java.io.IOException;

import com.badlogic.gdx.ApplicationListener;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.me.mygdxgame.Network.Obstacles;

public class GameClient implements ApplicationListener{
    Client client;
    String name;

    public GameClient () {
        client = new Client();
        client.start();

        // For consistency, the classes to be sent over the network are
        // registered by the same method for both the client and server.
        Network.register(client);

        client.addListener(new Listener() {
            public void connected (Connection connection) {
                System.out.println("connected");
            }

            public void received (Connection connection, Object object) {
                if (object instanceof Obstacles) {
                    Obstacles obs = (Obstacles)object;
                    System.out.println("Obstacle recieved on client - " + obs.obstacles.size());
                    return;
                }else {
                    System.out.println("invalid packet");
                }
            }

            public void disconnected (Connection connection) {
                EventQueue.invokeLater(new Runnable() {
                    public void run () {
                        client.close();
                        // Closing the frame calls the close listener which will stop the client's update thread.
                    }
                });
            }
        });

        final String host = "localhost";

        // We'll do the connect on a new thread so the ChatFrame can show a progress bar.
        // Connecting to localhost is usually so fast you won't see the progress bar.
        new Thread("Connect") {
            public void run () {
                try {
                    client.connect(5000, host, Network.port);
                    // Server communication after connection can go here, or in Listener#connected().
                } catch (IOException ex) {
                    ex.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }

    @Override
    public void create() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void render() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }
}

最佳答案

尝试使用client.SetKeepAliveTCP(int较小然后断开连接时间);

关于java - Kryonet 客户端超时后断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198360/

相关文章:

java - 有没有办法使用 Android Studio(基于 IntelliJ Idea 的 IDE)部署 Libgdx 桌面应用程序

java - 间隔树实现在序列化后占用大量文件空间

java - 在获取多行期间使用 spring-jdbctemplate 的 csv 输出

C# SHA-256 与 Java SHA-256。不同的结果?

android - 客户端-服务器 TCP 通信

java - 如何避免在 LIBGDX 中为单元测试编译着色器时出错?

java - LIbGDX 当语言改变时如何翻译所有文本?

另一种方法中的 Java 扫描器

delphi - Indy 10 Delphi FTP客户端演示错误

c - 如何停止我的 tcp echo 服务器以正确的方式工作?