将main()方法分开运行DesktopLauncher.main()和Server,使用Libgdx和Kryonet。

huangapple go评论108阅读模式
英文:

Separate main() methods to run DesktopLauncher.main() and Server using Libgdx and Kryonet

问题

I want my GameServer to run separately from the game itself. So, players(clients) can join into one static GameServer and I can handle them together and see how many clients are connected, currently.

But the problem is, I can only run only one of these classes(GameServer.main() and DesktopLauncher.main()) at the same time. GameServer must be running always at the background if I'm not wrong, right ? Yet, I can't run the game itself without stopping the GameServer. (It stucks saying Executing task 'DesktopLauncher.main()'...) I have some pictures to realize what's going on and what project structure looks like :

Pic 1 , Pic 2 , Pic 3

Here is my project structure :

core
-java
--com.mygdx.game
---Multiplayer
----Packets
-----GameClient.java
-----GameServer.java
-----GameClientListener.java
-----GameServerListener.java
---screens
---utils
---Application.java

GameServer.class

package com.mygdx.game.Multiplayer;


imports..

public class GameServer {

public int TCP_PORT,UDP_PORT;
public Server server;
public GameServerListener listener;

public static int totalClients = 0;

public GameServer() {

    TCP_PORT = UDP_PORT = xxxx;
    server = new Server(TCP_PORT,UDP_PORT);
    listener = new GameServerListener(this);

    startServer();
}


public void startServer() {

    server.addListener(listener);

    try {
        server.bind(TCP_PORT,UDP_PORT);
        //server.bind(TCP_PORT);

    } catch (IOException e) {
        e.printStackTrace();
    }
    registerPackets();
    server.start();

}

private void registerPackets() {
    server.getKryo().register(LoginRequest.class);
    server.getKryo().register(LoginResponse.class);
    server.getKryo().register(ChoiceRequest.class);
    server.getKryo().register(ChoiceRespond.class);

}

public static void main(String[] args) {
    new GameServer();
}

}

Thanks for any help.

英文:

I want my GameServer to run separately from the game itself. So, players(clients) can join into one static GameServer and I can handle them together and see how many clients are connected, currently.

But the problem is, I can only run only one of these classes(GameServer.main() and DesktopLauncher.main()) at the same time. GameServer must be running always at the background if I'm not wrong, right ? Yet, I can't run the game itself without stopping the GameServer. (It stucks saying Executing task 'DesktopLauncher.main()'...) I have some pictures to realize what's going on and what project structure looks like :

Pic 1 , Pic 2 , Pic 3

Here is my project structure :

> core
> -java
> --com.mygdx.game
> ---Multiplayer
> ----Packets
> -----GameClient.java
> -----GameServer.java
> -----GameClientListener.java
> -----GameServerListener.java
> ---screens
> ---utils
> ---Application.java

GameServer.class

package com.mygdx.game.Multiplayer;


imports..

public class GameServer {

public int TCP_PORT,UDP_PORT;
public Server server;
public GameServerListener listener;

public static int totalClients = 0;

public GameServer() {

    TCP_PORT = UDP_PORT = xxxx;
    server = new Server(TCP_PORT,UDP_PORT);
    listener = new GameServerListener(this);

    startServer();
}


public void startServer() {

    server.addListener(listener);

    try {
        server.bind(TCP_PORT,UDP_PORT);
        //server.bind(TCP_PORT);

    } catch (IOException e) {
        e.printStackTrace();
    }
    registerPackets();
    server.start();

}

private void registerPackets() {
    server.getKryo().register(LoginRequest.class);
    server.getKryo().register(LoginResponse.class);
    server.getKryo().register(ChoiceRequest.class);
    server.getKryo().register(ChoiceRespond.class);

}

public static void main(String[] args) {
    new GameServer();
}

}

Thanks for any help.

答案1

得分: 0

降级到 Android Studio 3.4 对我有效。

查看:https://www.reddit.com/r/libgdx/comments/fxrlsm/unable_to_run_two_or_more_application_in_parallel/

英文:

Had the same issue. Downgrading to Android Studio 3.4 worked for me.

Check: https://www.reddit.com/r/libgdx/comments/fxrlsm/unable_to_run_two_or_more_application_in_parallel/

huangapple
  • 本文由 发表于 2020年8月3日 04:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定