headless_server.adoc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. = jME3 Headless Server
  2. :revnumber: 2.0
  3. :revdate: 2020/07/24
  4. :keywords: server, spidermonkey, headless, network, documentation
  5. When adding multiplayer to your game, you may find that your server needs to know about game state (e.g. where are players, objects? Was that a direct hit? etc.) You can code all this up yourself, but there's an easier way.
  6. It's very easy to change your current (client) game to function as a server as well.
  7. == What Does Headless Mean?
  8. A headless server…
  9. * does not display any output – no window opens, no audio plays, no graphics are rendered.
  10. * ignores all input – no input handling.
  11. * keeps game state – you can attach to, transform, and save the rootNode, although the scene is not displayed.
  12. * calls the `simpleUpdate()` loop – you can run tests and trigger events as usual.
  13. == Client Code
  14. First, let's take a look at the default way of creating a new game (in its simplest form):
  15. [source,java]
  16. ----
  17. public static void main(String[] args) {
  18. Application app = new Main();
  19. app.start();
  20. }
  21. ----
  22. == Headless Server Code
  23. Now, with a simple change you can start your game in Headless mode. This means that all input and audio/visual output will be ignored. That's a good thing for a server.
  24. [source,java]
  25. ----
  26. import com.jme3.system.JmeContext;
  27. import com.jme3.system.JmeContext.Type;
  28. public static void main(String[] args) {
  29. Application app = new Main();
  30. app.start(JmeContext.Type.Headless);
  31. }
  32. ----
  33. == Next steps
  34. Okay, so you can now start your game in a headless "`server mode`", where to go from here?
  35. * Parse `String[] args` from the `main`-method to enable server mode on demand (e.g. start your server like `java -jar mygame.jar –server`.
  36. * Integrate xref:networking.adoc[SpiderMonkey], to provide game updates to the server over a network.
  37. * Only execute code that's needed. (E.g. place all rendering code inside an `if (servermode)`-block) (or `if (!servermode)` for the client).
  38. * Add decent xref:tutorials:how-to/java/logging.adoc[logging] so your server actually makes sense.