简体   繁体   English

Kryonet客户端在超时后断开连接

[英]Kryonet client disconnecting after timeout

My Kryonet server disconnects after 5000ms when I'm using this line for a connection client.connect(5000, host, Network.port); 当我使用这条线进行连接时,我的Kryonet服务器在5000毫秒后断开连接。client.connect client.connect(5000, host, Network.port); I thought the 5000 was the connection timeout but when I run the connection, it is able to connect and it receives the classes I send but it disconnects from the server after 5000ms. 我以为5000是连接超时,但是当我运行连接时,它可以连接并接收我发送的类,但是在5000ms之后它与服务器断开连接。

I'm modifying the basic ChatClient.java provided with Kryonet.. Here's what I came up with. 我正在修改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 smallthendisconnecttime);

The problem you are describing can arise if you configure your server for TCP and UDP but then have your client only connect via TCP. 如果将服务器配置为使用TCP UDP,但客户端通过TCP连接,则会出现您所描述的问题。

If you're wanting to take advantage of the host discovery but only need a TCP connection thereafter then it is advisable that you "run a separate server for UDP discovery" . 如果您想利用主机发现功能,但此后只需要TCP连接,那么建议您“为UDP发现功能运行单独的服务器”

Basically, you need to be sure that client and server protocol usage matches (either both server and client are setup for only TCP, or both server and client are setup for both TCP and UDP). 基本上,您需要确保客户端和服务器协议的使用情况相匹配(仅针对TCP设置服务器和客户端,或者针对TCP和UDP设置服务器和客户端)。

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

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