この回線を接続に使用しているときに、Kryonet サーバーが 5000 ミリ秒後に切断されます。5000
client.connect(5000, host, Network.port);
は接続タイムアウトだと思っていましたが、接続を実行すると、接続でき、送信したクラスを受信しますが、その後サーバーから切断されます。 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
}
}