我正在使用kryonet创建一个小型 JAVA 多人游戏来管理服务器/客户端部分。我有一个小问题:在我的服务器程序中,我用一个新的调用方法启动一个客户端,“on”一个布尔值:
Boolean xx = new clientprocess();
在我的客户端进程()中;,我得到了新的client();、client.connect和新客户端的类注册。它还包含 3 个方法conn/receive/disconn。现在,我不知道如何使用返回数据来获取布尔值。(事实上,我只是使用这个客户端连接到 BDD 服务器以获得通过/用户检查,所以我只需要一个布尔值来处理客户端。)
我试过停止和关闭客户端连接,删除监听器等等,但我无法摆脱Received();或断开连接();方法。而且因为它们是 Void 方法,所以我不能在方法中创建返回值。(我的实际返回是在clientprocess()结束时;在disconnected()之后;但从未到达过代码。)
有什么可以完全阻止客户端进程到达我的return boolean;
声明并继续我的登录过程吗?
这是我的代码的一部分,以Boolean xx = new loginProcess()
:
boolean loginProcess(final String usernamereceived, final String passwordreceived, final int todologin){
System.out.println("In LOGIN PROCESS");
final Client loginclient = new Client();
Kryo kryologin = loginclient.getKryo(); //encodage des packets
//kryo.register(byte[].class); Si on utilise le system de BYTE
kryologin.register(Packet0LoginRequest.class);
kryologin.register(Packet1LoginAnswer.class);
kryologin.register(Packet2Message.class);
kryologin.register(Packet2login.class);
kryologin.register(Packet2login1.class);
kryologin.register(Packet2login2.class);
kryologin.register(InetSocketAddress.class); //useless
kryologin.register(Inet4Address.class); //useless
kryologin.register(PacketupdateTest.class);
kryologin.register(LoginServerConnect.class);
kryologin.register(LoginServerConnectAnswer.class);
kryologin.register(java.util.ArrayList.class);
kryologin.register(java.util.List.class);
kryologin.register(User.class);
kryologin.register(String[].class);
System.out.println("Login registration over");
loginclient.start();
loginclient.addListener(new Listener() {
@Override
public void connected (Connection connection1) {
System.out.println("Connected login");
LoginServerConnect loginserverconnect = new LoginServerConnect();
connection1.sendTCP(loginserverconnect);
System.out.println("Loginserverconnect sended: "+loginserverconnect);
}
@Override
public void received (Connection connection1, Object object) {
System.out.println("Reception dun packet du login server HASH: "+object);
if (object instanceof LoginServerConnectAnswer) {
if (todologin==1){
System.out.println("LOGIN PROCESS");
Packet2login1 loginask = new Packet2login1();
loginask.Password = passwordreceived;
loginask.Username = usernamereceived;
connection1.sendTCP(loginask);
System.out.println("Fichier packet2login1 sended: User *"+loginask.Username+"*, et pass: *"+loginask.Password);
}
else if (todologin == 2){
System.out.println("ADD USER PROCESS");
Packet2AddUser adduserask = new Packet2AddUser();
adduserask.password = passwordreceived;
adduserask.username = usernamereceived;
connection1.sendTCP(adduserask);
System.out.println("Fichier packet2AddUser sended: user *"+adduserask.username+"*, et pass: *"+adduserask.password);
}
}
if (object instanceof Packet2login2){
Boolean result = ((Packet2login2) object).Result;
String usernamefromlogin = ((Packet2login2) object).Username;
System.out.println("reception de la reponse de login: **"+result+"** pour l'utilisateur: "+usernamefromlogin);
//creation nouvel user - ajout if useramefromlogin == usernamereceived
user.name = usernamefromlogin;
if(result){
System.out.println("user received from login server: "+ user.name+", and KryoId: "+Kryoid); System.out.println("Kryoid du client actu (depuis le packet0 C-S: "+ Kryoid);
int lastid = players.lastIndexOf(user);
if (players.lastIndexOf(user) == 0){
user.Id = lastid;
}
else {user.Id = lastid +1;}
players.add(user);
System.out.println("Players.add over");
System.out.println("BDD result returned0: "+mtx);
mtx = true;
System.out.println("BDD result returned1: "+mtx);
/* connection1.close();
loginclient.close();
loginclient.stop();*/
loginclient.removeListener(this);
}
else{
System.out.println("Username or Password incorrect for user: "+ usernamefromlogin);
mtx = false;
}
}
if (object instanceof Packet2AddUser1){
Boolean addresult = ((Packet2AddUser1) object).adduserresult;
String usernamefromlogin = ((Packet2AddUser1) object).username;
if(addresult){
System.out.println("Nouvel utilisateur enregistre en BDD: *"+ usernamefromlogin);
mtx = true;
}
else {
System.out.println("Erreur lors de lenregistrement en BDD pour: *"+ usernamefromlogin);
mtx = false;
}
}
System.out.println("END of received");
}
@Override
public void disconnected (Connection connection1){
System.out.println("public void DISCONN passé OK cote LISTENER");
}
});
try {
System.out.println("loginclient.connect");
loginclient.connect(loginstimeout, loginsip, loginsPORT);
System.out.println("end of conn");
} catch (IOException ex) {
Logger.getLogger(NetworkListener.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("BDD result returned: "+mtx);
return mtx;
}